Multiple grouped summaries — calculating different statistics for different columns within each group — is one of the most powerful patterns in data analysis. In Pandas, you combine groupby() with .agg() and a dictionary to apply exactly the aggregations you want per column, per group.
In 2026, this technique is essential for dashboards, cohort analysis, financial reporting, A/B testing, and any time you need rich group-level insights. Here's a practical guide with real examples you can copy and adapt.
1. Basic Setup & Sample Data
import pandas as pd
data = {
'Group': ['A', 'B', 'C', 'A', 'B', 'C'],
'Value1': [1, 2, 3, 4, 5, 6],
'Value2': [10, 20, 30, 40, 50, 60],
'Sales': [100, 200, 150, 300, 250, 400]
}
df = pd.DataFrame(data)
print(df)
2. Grouped Summary with Different Functions per Column
Use a dictionary inside .agg(): keys = column names, values = aggregation function (or list of functions).
grouped_multi = df.groupby('Group').agg({
'Value1': 'mean', # single function
'Value2': ['sum', 'mean'], # multiple functions
'Sales': 'sum'
})
print(grouped_multi)
Output (multi-level columns):
Value1 Value2 Sales
mean sum mean sum
Group
A 2.5 50 25.0 400
B 3.5 70 35.0 450
C 4.5 90 45.0 550
3. Clean Output with Named Aggregations
Use NamedAgg or alias syntax for flat, readable column names.
from pandas import NamedAgg
named_summary = df.groupby('Group').agg(
avg_value1=('Value1', 'mean'),
total_value2=('Value2', 'sum'),
avg_value2=('Value2', 'mean'),
grand_total_sales=('Sales', 'sum')
)
print(named_summary)
Output (clean & flat):
avg_value1 total_value2 avg_value2 grand_total_sales
Group
A 2.5 50 25.0 400
B 3.5 70 35.0 450
C 4.5 90 45.0 550
4. Multiple Functions + Renaming (Advanced)
Use lists inside the dictionary and rename afterward for full control.
raw_agg = df.groupby('Group').agg({
'Value1': ['mean', 'min', 'max'],
'Value2': ['sum', 'mean'],
'Sales': 'sum'
})
# Flatten multi-level columns and rename
raw_agg.columns = ['_'.join(col).strip() for col in raw_agg.columns.values]
raw_agg = raw_agg.rename(columns={
'Value1_mean': 'avg_value1',
'Value1_min': 'min_value1',
'Value1_max': 'max_value1',
'Value2_sum': 'total_value2',
'Value2_mean': 'avg_value2',
'Sales_sum': 'total_sales'
})
print(raw_agg)
5. Modern Alternative in 2026: Polars
For large datasets, Polars is often faster and more memory-efficient with similar syntax.
import polars as pl
df_pl = pl.DataFrame(data)
grouped_pl = df_pl.group_by("Group").agg(
avg_value1=pl.col("Value1").mean(),
total_value2=pl.col("Value2").sum(),
total_sales=pl.col("Sales").sum()
)
print(grouped_pl)
Best Practices & Common Pitfalls
- Use dictionary syntax for different functions per column — much clearer
- Handle missing data before aggregation (
fillnaordropna) - Reset index after groupby if you want 'Group' as a regular column:
.reset_index() - For huge data, prefer Polars or chunked processing
- Visualize results:
grouped_multi.plot(kind='bar')for instant insights
Conclusion
Multiple grouped summaries with groupby() + .agg() turn raw data into rich, actionable insights — averages, totals, counts, min/max per group. In 2026, use Pandas for readability on small-to-medium data, and Polars for speed on large datasets. Master dictionary aggregations, NamedAgg, and custom functions, and you'll write concise, powerful summaries that scale from exploration to production.
Next time you need group-level metrics across multiple columns — reach for groupby + agg first.