

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Aggregation
incomplete
2: Grouping With Dictionaries
incomplete
3: Grouping in Pandas
incomplete
4: Category Type
incomplete
5: Grouping by Multiple Columns
incomplete
6: Multiple Aggregations
incomplete
7: Named Aggregations
incomplete
8: Custom Aggregations
incomplete
9: Pivot Tables
incomplete
10: Pivot Table Aggregations
incomplete
11: Star Schema
incomplete
12: Grain Validation
incomplete
13: Fixing Grain Violations
incomplete
14: Building a Data Model
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Often you need more than one summary per group: total revenue, average order value, and largest sale all at once. The .agg() method lets you apply multiple aggregation functions in a single call: just pass a list of function names.
orders = pd.DataFrame(
{
"category": [
"Electronics",
"Electronics",
"Clothing",
"Clothing",
"Electronics",
],
"revenue": [500, 300, 200, 400, 150],
}
)
summary = orders.groupby("category")["revenue"].agg(["sum", "mean", "max"])
print(summary)
# sum mean max
# category
# Clothing 600 300.000000 400
# Electronics 950 316.666667 500
The result is a DataFrame with one column per aggregation, each named after the function that produced it.
These strings work with .agg():
| Function | What it does |
|---|---|
'sum' |
Total |
'mean' |
Average |
'count' |
Number of non-null values |
'size' |
Number of rows |
'min' |
Minimum value |
'max' |
Maximum value |
'std' |
Standard deviation |
'nunique' |
Number of unique values |
count excludes NaN values, while size includes them. If your data has missing values, the difference matters.
SnackStack's reliability team wants a temperature health check.
Complete the summarize_temperature_by_type function. It accepts a DataFrame with a temperature_celsius column, and returns one summary row per device type.