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.
Multiple aggregation functions in 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.
df.groupby('Category').agg(['mean', 'sum'])
df.groupby('Team')['Points'].agg(['min', 'max', 'mean'])
df.groupby('City').agg({'Temperature': ['min', 'max'], 'Rainfall': 'sum'})
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.
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)
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.
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.
Practice
agg() function do when used after groupby() in pandas?Solution
Step 1: Understand groupby operation
Thegroupby()function splits data into groups based on column values.Step 2: Apply
Theagg()after groupingagg()function applies one or more aggregation functions like sum, mean, count to each group and returns the results.Final Answer:
It applies multiple aggregation functions to each group and returns their results. -> Option DQuick Check:
agg()summarizes groups [OK]
- Thinking agg() sorts data
- Confusing agg() with filtering
- Assuming agg() merges DataFrames
sum and mean aggregations on a grouped DataFrame df grouped by column 'Category'?Solution
Step 1: Identify correct method chaining
Grouping must happen first withgroupby('Category'), then aggregation withagg().Step 2: Use list of functions inside
The correct syntax uses a list of function names:agg()agg(['sum', 'mean']).Final Answer:
df.groupby('Category').agg(['sum', 'mean']) -> Option AQuick Check:
Group first, then agg with list [OK]
- Calling agg() before groupby()
- Passing multiple args instead of list to agg()
- Using aggregate() without list syntax
import pandas as pd
data = {'Team': ['A', 'A', 'B', 'B'], 'Points': [10, 15, 10, 20], 'Assists': [5, 7, 8, 6]}
df = pd.DataFrame(data)
result = df.groupby('Team').agg({'Points': ['sum', 'mean'], 'Assists': 'max'})
print(result)What is the output?
Solution
Step 1: Group by 'Team'
The data is grouped into two groups: Team A and Team B.Step 2: Apply multiple aggregations
For 'Points', sum and mean are calculated; for 'Assists', max is calculated for each group.Final Answer:
A DataFrame with sum and mean of Points and max of Assists for each Team. -> Option AQuick Check:
agg() supports dict with list and string [OK]
- Expecting error mixing list and string in agg dict
- Confusing which function applies to which column
- Thinking output is a Series, not DataFrame
df.groupby('Category').agg({'Sales': 'sum', 'Profit': ['mean', 'max']})It raises an error. Why?
Solution
Step 1: Check agg() dictionary syntax
agg() accepts a dictionary where values can be a string or list of functions per column.Step 2: Verify code correctness
The code correctly applies 'sum' to 'Sales' and ['mean', 'max'] to 'Profit' without error.Final Answer:
The code is correct and runs without error. -> Option CQuick Check:
agg() accepts mixed string and list in dict [OK]
- Thinking mixing string and list causes error
- Expecting all functions must be in a list
- Confusing agg() with apply() syntax
df with columns 'Store', 'Sales', and 'Expenses'. You want to group by 'Store' and get the total sales, average expenses, and count of sales entries per store in one step. Which code correctly does this?Solution
Step 1: Identify required aggregations per column
We want total sales (sum), average expenses (mean), and count of sales entries (count).Step 2: Map functions correctly in agg()
Use a dict: 'Sales' gets ['sum', 'count'], 'Expenses' gets 'mean'. This matches df.groupby('Store').agg({'Sales': ['sum', 'count'], 'Expenses': 'mean'}).Final Answer:
df.groupby('Store').agg({'Sales': ['sum', 'count'], 'Expenses': 'mean'}) -> Option BQuick Check:
Dict with list and string per column [OK]
- Putting count under wrong column
- Using agg() without dict for columns
- Applying all functions to all columns
Ready to prove it?
Pass the test and this pattern moves unknown
