

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
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
A pivot table can calculate more than one summary at a time, which is useful when a report needs multiple views of the same data, like:
Pass a list of functions to aggfunc:
tickets = pd.DataFrame(
{
"team": ["Support", "Support", "Billing", "Billing", "Support", "Billing"],
"priority": ["high", "low", "high", "low", "high", "low"],
"response_minutes": [45, 12, 60, 20, 30, 25],
}
)
# Calculate both mean and max
pivot = pd.pivot_table(
tickets,
values="response_minutes",
index="team",
columns="priority",
aggfunc=["mean", "max"],
)
print(pivot)
# mean max
# priority high low high low
# team
# Billing 60.0 22.5 60 25
# Support 37.5 12.0 45 12
The result has a set of columns for each aggregation function. Read it in two layers:
mean or max.high or low.So mean -> high means average response time for high-priority tickets, while max -> high means the slowest high-priority response time.
Multiple pivot aggregations are useful for compact report tables, since they let you compare related metrics without building several separate pivots. But they can create awkward column labels.
If you need clean column names for more analysis, a named groupby().agg() is often easier to work with.
SnackStack's operations team wants one compact energy report: for every region, both the total and average power draw of each device type, side by side.
Complete the energy_summary_by_region_and_type function. It accepts a DataFrame with region, device_type, and energy_kwh columns and returns a pivot table.