0
0
Pandasdata~10 mins

Multiple aggregation functions in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple aggregation functions
Start with DataFrame
Choose column(s) to aggregate
Apply multiple aggregation functions
Get aggregated results for each function
Combine results into one DataFrame or Series
Use or display aggregated output
We start with data, select columns, apply several aggregation functions at once, and get combined results.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})
result = df.agg({'A': ['sum', 'mean'], 'B': ['min', 'max']})
print(result)
This code applies sum and mean to column A, min and max to column B, and prints the combined results.
Execution Table
StepActionColumnAggregation FunctionResult
1Select column 'A'A-[1, 2, 3, 4]
2Select column 'B'B-[5, 6, 7, 8]
3Apply 'sum' to 'A'Asum10
4Apply 'mean' to 'A'Amean2.5
5Apply 'min' to 'B'Bmin5
6Apply 'max' to 'B'Bmax8
7Combine all resultsA, Bsum, mean, min, maxDataFrame with results
8Print result-- A B sum 10.0 NaN mean 2.5 NaN min NaN 5.0 max NaN 8.0
💡 All specified aggregation functions applied and results combined into one output.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
df{'A':[1,2,3,4],'B':[5,6,7,8]}SameSameSameSameSame
resultNonePartial sum for A=10Sum and mean for A=10,2.5Sum, mean A; min B=5Sum, mean A; min, max B=5,8Full DataFrame with all aggregations
Key Moments - 2 Insights
Why do some cells in the result DataFrame show NaN?
Because each aggregation function applies only to specified columns, other columns have no value for that function, so pandas fills with NaN (see execution_table rows 7 and 8).
Can we apply different aggregation functions to different columns at once?
Yes, by passing a dictionary to agg() with column names as keys and list of functions as values, as shown in the code and execution_table steps 3-6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the mean of column 'A'?
A10
B3
C2.5
DNaN
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step is the minimum value of column 'B' calculated?
AStep 3
BStep 5
CStep 6
DStep 7
💡 Hint
Look for 'min' aggregation on column 'B' in the execution_table.
If we add 'max' aggregation to column 'A', how will the final result change?
AA new row with max value for 'A' will appear
BThe 'max' value will replace the 'sum' value for 'A'
CNo change, max is ignored
DThe entire result will be empty
💡 Hint
Adding an aggregation function adds a new row for that function in the result (see execution_table step 7).
Concept Snapshot
Use df.agg() to apply multiple aggregation functions at once.
Pass a dict: keys are columns, values are list of functions.
Result is a DataFrame with functions as rows, columns as columns.
NaN appears where function doesn't apply to a column.
Useful for quick summary stats on multiple columns.
Full Transcript
We start with a DataFrame containing columns A and B. We want to apply multiple aggregation functions to these columns at once. We select columns A and B, then apply sum and mean to A, and min and max to B using the agg() method with a dictionary. Each function is applied step-by-step, producing partial results. These results are combined into one DataFrame where rows represent aggregation functions and columns represent original columns. Some cells show NaN because not all functions apply to all columns. This combined output is printed as the final result. This method helps quickly get multiple summary statistics in one call.