All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 4 · about 20 minutes · uses Lesson 4.1. You’ll be able to build a summary table with many measures in one step.

agg computes several summaries per group at once, and named aggregation gives each result a clear column name ready for a report.

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

Pythonorders.csv

2. Named aggregations: the report-ready way

new_column=(source_column, function) for each measure:

Pythonorders.csv

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:

Pythonorders.csv

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:

The rows.
storecustomerqty
S01C12
S01C23
S01C11
WEBC34
Ask for three facts. agg(units=('qty', 'sum'), lines=('qty', 'size'), customers=('customer', 'nunique'))
S01. units 6, lines 3, customers 2 (C1 counted once).
Result.
storeunitslinescustomers
S01632
WEB411

4. Your turn

Your turnproducts.csv
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

  1. Per customer: number of orders, total revenue and last order date.
  2. Why compute revenue per order after agg, not as a mean of per-line values?
  3. What does agg(['min', 'max']) produce as column names?
Answers
  1. orders.groupby('customer_id').agg(orders=('order_id', 'nunique'), revenue=('revenue', 'sum'), last=('date', 'max')).
  2. A line isn’t an order; per-order value needs the order’s total, so divide total revenue by the number of orders.
  3. Columns named min and max (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

1What does .agg(['sum', 'mean']) on one column give?
2Which is a named aggregation?
3Why prefer named aggregations for reports?

Sources

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.