Module 4: Grouping and aggregating · Lesson 4.2 · about 20 min
Several summaries at once with agg
Use agg to compute many summaries in one groupby: several functions on one column, different functions per column, and named aggregations that give clean column names for reports.
Module 4 · about 20 minutes · uses Lesson 4.1. You’ll be able to build a summary table with many measures in one step.
In everyday terms: one trip to each pile of receipts, writing down the total, the number of receipts and the biggest one, instead of three separate trips.
1. Several functions on one column
2. Named aggregations: the report-ready way
new_column=(source_column, function) for each measure:
This is the shape of most KPI tables: one row per group, one column per measure, ratios computed after.
3. Your own functions
Any function that takes a Series and returns one value works, including a lambda:
Built-in names ('sum', 'mean') are much faster than lambdas on big tables; use lambdas for things without a built-in.
- agg(['sum', 'mean'])
- Several functions on one column.
- agg(units=('qty', 'sum'), lines=('qty', 'size'))
- Named aggregation: new name = (column, function).
- Custom function
- Any function that takes a group's values and returns one number.
- Physical meaning
- A report row per group: "Harbourview: 1,204 units, 610 lines, 312 customers". Each named result is one fact about the group, computed from its rows.
“The mean of group means is the overall mean.” Only if every group is the same size. Compute the overall figure from the totals (Lesson 8.2).
Watch it happen, one step at a time:
| store | customer | qty |
|---|---|---|
| S01 | C1 | 2 |
| S01 | C2 | 3 |
| S01 | C1 | 1 |
| WEB | C3 | 4 |
agg(units=('qty', 'sum'), lines=('qty', 'size'), customers=('customer', 'nunique'))| store | units | lines | customers |
|---|---|---|---|
| S01 | 6 | 3 | 2 |
| WEB | 4 | 1 | 1 |
4. Your turn
How it works
Principle: agg runs each (column, function) pair once per group and lays the answers side by side. Measures that are ratios (revenue per order, conversion rate) must be computed from the summed parts after aggregating, not by averaging per-row ratios, or small groups get the same weight as big ones.
Example: two stores with conversion 10 % (of 100 visitors) and 2 % (of 10,000). Average of rates: 6 %. True combined rate: 210 ÷ 10,100 ≈ 2.1 %.
5. Practice
- Per customer: number of orders, total revenue and last order date.
- Why compute revenue per order after
agg, not as a mean of per-line values? - What does
agg(['min', 'max'])produce as column names?
Answers
orders.groupby('customer_id').agg(orders=('order_id', 'nunique'), revenue=('revenue', 'sum'), last=('date', 'max')).- A line isn’t an order; per-order value needs the order’s total, so divide total revenue by the number of orders.
- Columns named
minandmax(with two-level names if you used it on several columns).
New words: agg, named aggregation, summary table, ratio of sums.
- agg runs several summaries per group in one pass.
- Named aggregation: new_name=(column, function).
- size counts rows; count skips NaN; nunique counts distinct values.
- Custom functions work but are slower than built-ins.
Check yourself
Sources
- pandas documentation, User Guide: Group by (aggregation) https://pandas.pydata.org/docs/user_guide/groupby.html#aggregation
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.