Page 78 - Informatics_Practices_Fliipbook_Class12
P. 78
Next, we wish to find average, maximum, and minimum price of items in each category, so, we apply aggregate
functions mean, min, and max on column Price of grouped DataFrame groceryGroupedDF, as shown below:
>>> groceryGroupedDF['Price'].agg(['mean', 'min', 'max'])
output:
mean min max
Category
Food 42.500000 20 70
Household 80.000000 80 80
Hygiene 33.333333 30 40
Execution of
groceryGroupedDF['Price'].agg(['mean', 'min', 'max'])
in the Pandas tutor yields the following visualization:
groceryGroupedDF['Price'].agg(['mean', 'min', 'max'])
Series mean min max
0 20 Category
1 60 Food 42.50 20 70
2 20 Household 80 80 80
3 70 Hygiene 33.33 30 40
4 80
5 40
6 30
7 30
We can organise the data in a DataFrame into groups using the function groupby(), based on specific criteria,
such as values stored in one or more columns. The method returns a DataFrameGroupBy object.
We can apply more than one aggregate operations by passing the list of operations to be carried out on the groups
of data as an argument to the function agg.
C T 04 Write the Pandas statements to group the records of employeeDF DataFrame with respect
to department and compute minimum, maximum, and average salary of employees in each
department.
Let's Summarise
A Pandas DataFrame is a two-dimensional tabular structure that can accommodate objects of various types.
Ø
A dictionary can be converted to a Pandas DataFrame using the method pd.DataFrame(). The keys of
Ø
the dictionary act as column labels. For each key (acting as a label), the values in the list appear in the
corresponding column in the DataFrame.
A list of lists can be converted to a Pandas DataFrame using the method pd.DataFrame(). Each sublist in
Ø
the list includes the values in a row of the DataFrame.
64 Touchpad Informatics Practices-XII

