

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 6
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
groupby() is the built-in way to aggregate in Pandas. It uses the split-apply-combine pattern.
orders = pd.DataFrame(
{
"region": ["West", "East", "West", "East", "West"],
"revenue": [100, 200, 150, 300, 250],
}
)
revenue_by_region = orders.groupby("region")["revenue"].sum()The result:
region
East 500
West 500Here's what happened:
Split – Pandas grouped the rows by region (West rows together, East rows together)
Apply – .sum() ran on each group's revenue column
Combine – Pandas returned the results as a single Series, with the group keys (region) as its index
You can run other aggregation functions as well, once you've grouped the data:
orders.groupby("region")["revenue"].mean() # average
orders.groupby("region")["revenue"].count() # number of non-null values
orders.groupby("region")["revenue"].min() # smallest value
orders.groupby("region")["revenue"].max() # largest valueSnackStack wants a dashboard tile showing how hot each region's devices are running on average.
Complete the average_temperature_by_region function. It accepts a DataFrame with a temperature_celsius column, and returns a tidy DataFrame with one row per region.