32 — Data Analytics

GroupBy, Pivot & Aggregation#

Analytics✓ Mathematical
◆ The PatternSplit-apply-combine — the single most-used pattern in data analytics

Nearly every analytics question is "metric X by segment Y" — revenue by region, churn by plan, latency by endpoint. The engine underneath is always split-apply-combine: split rows into groups, apply an aggregation to each, combine the results into a table. pandas groupby, SQL GROUP BY, and spreadsheet pivot tables are the same idea in three dialects.

split(rows, key) → apply(agg) → combine
Split-apply-combine — group rows by key, reduce each group with mean/sum/count/…, stack the results.
// Interactive — raw rows on the left, one aggregated bar per group on the right
Aggregationmean
ToolBest forOne-liner
groupby + aggMetrics by one or more keysdf.groupby('region')['rev'].sum()
pivot_tableTwo keys → rows × columns griddf.pivot_table('rev', 'region', 'month')
crosstabCounts of category × categorypd.crosstab(df.plan, df.churned)
resampleTime-based groupingdf.resample('W')['rev'].sum()
transformGroup stat broadcast back to rowsg['rev'].transform('mean')
# Python — the aggregation toolbox
import pandas as pd

# Multiple metrics per group, named columns
summary = (df.groupby('region')
             .agg(orders=('order_id', 'count'),
                  revenue=('amount', 'sum'),
                  avg_basket=('amount', 'mean'))
             .sort_values('revenue', ascending=False))

# Rows × columns grid
pivot = df.pivot_table(values='amount', index='region',
                       columns='month', aggfunc='sum', fill_value=0)

# Same thing in SQL
# SELECT region, COUNT(*), SUM(amount), AVG(amount)
# FROM orders GROUP BY region ORDER BY SUM(amount) DESC;

# Group stat next to each row (for "% of segment" columns)
df['pct_of_region'] = df['amount'] / df.groupby('region')['amount'].transform('sum')
Mean of means ≠ overall mean. Averaging per-group averages weights every group equally regardless of size. Aggregate from raw rows (or weight by group size) — this is Simpson's paradox waiting to happen in a dashboard.
Pattern bridge: Aggregation is also feature engineering — per-entity groupby stats ("customer's average order", "merchant's txn count") are among the strongest features in tabular ML and fraud models.
When to use this
Use when: Building any report, dashboard metric, or segment comparison — and when engineering aggregate features for models.
Skip when: You need row-level detail (aggregation destroys it) — or the "groups" are time windows with order mattering, where rolling windows beat plain groupby.
Try it on real data
Kaggle: Superstore Sales (10K orders — region, category, segment) UCI: Online Retail (540K transactions, invoice-level)
Superstore is the canonical groupby playground: every business question is a two-line aggregation away.
Quick start — copy to notebook
pip install pandas seaborn
# ────────────────────────────────────────
import seaborn as sns

tips = sns.load_dataset('tips')

summary = (tips.groupby(['day', 'time'], observed=True)
               .agg(orders=('total_bill', 'count'),
                    revenue=('total_bill', 'sum'),
                    avg_bill=('total_bill', 'mean'))
               .round(2))
print(summary)

print(tips.pivot_table('total_bill', index='day',
                       columns='time', aggfunc='sum', observed=True))
← Previous
Exploratory Data Analysis (EDA)
Open in the full reader, with the topic sidebar →