Page 75 - Informatics_Practices_Fliipbook_Class12
P. 75
>>> groceryDF['Total Price'] = Total
>>> groceryDF.to_csv('GroceryV2.csv', header= True)
Note that the function call groceryDF.to_csv('GroceryV2.csv', header= True) saves the file in the current
directory. Alternatively, complete pathname may be specified. Further, if the keyword argument header = True is
not included, the DataFrame contents will still be transferred to the file, but without the column names.
The DataFrame object can be saved as CSV (Comma-Separated Values) file using the to_csv() function of Pandas
DataFrame:
df.to_csv('output.csv', index=False)
C T 04 Write the Pandas statements to save the updated employeeDF DataFrame to csv file
employeeDFupdated.csv.
2.10 Grouping and Aggregation
Often, we need to analyse data group wise. For example, we may like to examine the purchases in different categories.
We may also want to aggregate the data group wise. For example, it would be interesting to know the sum or mean
of the amount spent on purchases in each category. Similarly, one may like to know the count of purchases in each
category. Before, proceeding further, let us compute the total price paid for each item and add the column Total
Price to the DataFrame groceryDF, as shown below:
>>> groceryDF = pd.read_csv('Grocery.csv')
>>> # Multiplying Price and Quantity values to get Total Price for each item
>>> TotalPrice = groceryDF['Price'] * groceryDF['Quantity']
>>> # Adding a new Column to the Dataframe
>>> groceryDF['Total Price'] = TotalPrice
>>> print(groceryDF.head())
Product Category Price Quantity Total Price
0 Bread Food 20 2 40
1 Milk Food 60 5 300
2 Biscuit Food 20 2 40
3 Bourn-Vita Food 70 1 70
4 Soap Hygiene 40 4 160
2.10.1 Grouping
As mentioned above, sometimes, it is useful to organise the data in a DataFrame into groups based on specific criteria,
such as values stored in one or more columns. To begin with, let us organise the data in the DataFrame groceryDF
category wise, using the function groupby().
#Groupby in Pandas
#All products grouped according to the category
>>> groceryGroupedDF = groceryDF.groupby('Category')
>>> print(groceryGroupedDF)
>>> <pandas.core.groupby.generic.DataFrameGroupBy object at 0x7e6816317100>
Now, we have created a DataFrameGroupBy object that can be used to perform operations on each group. To begin
with, we use the object groceryGroupedDF to perform iteration group-wise.
Each group is described by a tuple comprising the value that defines the group and a DataFrame object comprising the
associated part of the groceryGroupedDF, as shown below:
>>> for record in groceryGroupedDF:
>>> print(type(record), len(record), record[0], type(record[1]))
Data Handling using Pandas DataFrame 61

