Grouping and Aggregations (groupby, agg)
The idea
groupbygroupby answers questions like:
- Average revenue per city?
- Total orders per customer?
- Highest score per class?
Think: split → apply → combine.
Example dataset
Sales sample
import pandas as pd
sales = pd.DataFrame({
"city": ["pune", "pune", "delhi", "delhi", "delhi"],
"category": ["A", "B", "A", "A", "B"],
"amount": [100, 200, 150, 120, 300],
})
print(sales)Sales sample
import pandas as pd
sales = pd.DataFrame({
"city": ["pune", "pune", "delhi", "delhi", "delhi"],
"category": ["A", "B", "A", "A", "B"],
"amount": [100, 200, 150, 120, 300],
})
print(sales)Basic aggregations
Sum per city
Sum per city
print(sales.groupby("city")["amount"].sum())Sum per city
print(sales.groupby("city")["amount"].sum())Mean per city
Mean per city
print(sales.groupby("city")["amount"].mean())Mean per city
print(sales.groupby("city")["amount"].mean())Multiple aggregations with aggagg
Multiple metrics
summary = (
sales.groupby("city")
.agg(
total_amount=("amount", "sum"),
avg_amount=("amount", "mean"),
orders=("amount", "count"),
)
)
print(summary)Multiple metrics
summary = (
sales.groupby("city")
.agg(
total_amount=("amount", "sum"),
avg_amount=("amount", "mean"),
orders=("amount", "count"),
)
)
print(summary)Group by multiple keys
Group by city and category
summary = (
sales.groupby(["city", "category"])
.agg(total_amount=("amount", "sum"), orders=("amount", "count"))
.reset_index()
)
print(summary)Group by city and category
summary = (
sales.groupby(["city", "category"])
.agg(total_amount=("amount", "sum"), orders=("amount", "count"))
.reset_index()
)
print(summary)Common gotchas
- After
groupbygroupby, the grouped columns become the index. Use.reset_index().reset_index()if you want them as normal columns. - Aggregation functions ignore missing numeric values by default (
NaNNaN) in many cases.
Mental model
When you write:
python
sales.groupby("city")["amount"].sum()python
sales.groupby("city")["amount"].sum()You are saying:
- Split the rows by
citycity - Select the
amountamountcolumn - Sum within each group
This pattern comes up constantly in analytics.
Visualize it
flowchart LR A["Original rows"] --> B["Split by key (city)"] B --> C1["Group: pune"] B --> C2["Group: delhi"] C1 --> D1["Apply sum()"] C2 --> D2["Apply sum()"] D1 --> E["Combine into result"] D2 --> E
🧪 Try It Yourself
Exercise 1 – Group and Sum
Exercise 2 – Multiple Aggregations With agg
Exercise 3 – Reset the Index After Grouping
Next
groupbygroupby and aggagg cover the essentials — continue to Advanced GroupBy (transform,
filter, named agg) for broadcasting group stats back, filtering whole groups, and
bucket analysis.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
