GroupBy & Aggregation
Part of the Machine Learning Roadmap
In short
Which city orders the most? Which product returns the most? Average spend per user? Almost every useful question about data starts with 'per something', and groupby answers it.
groupby splits data into groups, applies a function like mean or count, and combines the results. It answers questions like average order value per city and builds aggregate features for models.

Think of it like this
The dhobi sorting laundry
A huge pile of clothes arrives. The dhobi first sorts them into baskets by colour, washes each basket the right way, then stacks the folded results together. Split into groups, apply a calculation, combine the answers. That is exactly how groupby works.
Watch it happen
sales.groupby('city')['amount'].sum()
1/4Five orders: Pune 200, Delhi 450, Pune 300, Delhi 150, Goa 500.
The big ideas
What you need to know
Split, apply, combine
Every groupby does these three steps. Picture them and the output shape stops surprising you.
Named aggregations
.agg(total=('amount', 'sum'), orders=('amount', 'count')) gives tidy, readable column names.
Group by several columns
groupby(['city', 'month']) gives one row per city-month pair. Great for trends per segment.
transform keeps the shape
transform('mean') returns a value for every original row, perfect for features like 'user's average spend'.
See it in code
Walk through the code
1import pandas as pd2 3sales = pd.DataFrame({4 "city": ["Pune", "Delhi", "Pune", "Delhi", "Goa"],5 "amount": [200, 450, 300, 150, 500],6})7 8print(sales.groupby("city")["amount"].sum()) # Delhi 600, Goa 500, Pune 5009 10summary = sales.groupby("city").agg(11 total=("amount", "sum"), orders=("amount", "count"), avg=("amount", "mean"),12).reset_index()13 14sales["city_avg"] = sales.groupby("city")["amount"].transform("mean")15print(summary)16print(sales)1/4Five orders across three cities.
agg makes a summary table with one row per city. transform adds the city average back onto every order, a common feature engineering trick.
Save yourself the pain
Common mistakes
Oops
Using agg when you want a value per original row, then struggling to merge it back.
Do this instead
Use transform. It returns a result aligned with the original rows.
Oops
Forgetting that groupby drops rows whose key is NaN by default.
Do this instead
Pass dropna=False if missing keys should form their own group.
Your mission
Find the best tipping day
Quick check
You want a new column with each customer's average order value on every one of their order rows. Which method fits?
Go deeper
- 1Compute average, total and count of orders per city with one groupby and agg call.
- 2Use transform to add each row's group average as a new feature.
- 3Try pivot_table for a two-way summary like city by month.
Also part of these roadmaps
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.