We use multiple aggregation functions to get different summary numbers from data at once. It helps us understand data better by showing many insights in one step.
0
0
Multiple aggregation functions in Pandas
Introduction
When you want to see both the average and the total of sales for each product.
When you need to find the minimum and maximum temperatures recorded each day.
When summarizing survey results by calculating count, mean, and standard deviation.
When analyzing student scores to get the average, highest, and lowest marks per class.
Syntax
Pandas
df.groupby('column').agg(['func1', 'func2', ...])
You can use built-in functions like 'mean', 'sum', 'min', 'max', or your own functions.
The result is a DataFrame with multiple columns for each aggregation function.
Examples
This groups data by 'Category' and calculates the mean and sum for each group.
Pandas
df.groupby('Category').agg(['mean', 'sum'])
This groups by 'Team' and calculates minimum, maximum, and average points.
Pandas
df.groupby('Team')['Points'].agg(['min', 'max', 'mean'])
This applies different functions to different columns after grouping by 'City'.
Pandas
df.groupby('City').agg({'Temperature': ['min', 'max'], 'Rainfall': 'sum'})
Sample Program
This code groups the data by 'Team' and calculates the mean and sum for 'Points' and 'Assists'. It shows how multiple aggregation functions work together.
Pandas
import pandas as pd data = {'Team': ['A', 'A', 'B', 'B', 'C', 'C'], 'Points': [10, 15, 10, 20, 15, 25], 'Assists': [5, 7, 8, 6, 7, 9]} df = pd.DataFrame(data) result = df.groupby('Team').agg(['mean', 'sum']) print(result)
OutputSuccess
Important Notes
Aggregation functions are applied to each column separately.
You can pass a dictionary to apply different functions to different columns.
Result columns become multi-level with function names as second level.
Summary
Multiple aggregation functions let you get many summaries in one step.
Use agg() with a list or dictionary of functions after grouping.
Results show each function's output clearly for each group.