The .agg() method is one of the most powerful and flexible tools in Pandas for summarizing data — especially when you need to apply multiple aggregation functions at once, or different functions to different columns. In 2026, it's still the go-to method for clean, expressive groupby aggregations and quick DataFrame-wide summaries.
Whether you're calculating sales totals, user metrics, sensor statistics, or financial summaries, .agg() lets you do it all in one readable line.
Basic Usage: Multiple Aggregations on Selected Columns
Use .agg() directly on a DataFrame or after a groupby() to apply several functions at once.
import pandas as pd
data = {
'Name': ['John', 'Mary', 'Peter', 'Anna', 'Mike'],
'Age': [25, 32, 18, 47, 23],
'Salary': [50000, 80000, 35000, 65000, 45000]
}
df = pd.DataFrame(data)
# Apply multiple stats to Age and Salary
summary = df[['Age', 'Salary']].agg(['mean', 'median', 'min', 'max', 'std'])
print(summary)
Output:
Age Salary
mean 29.0000 55000.0000
median 25.0000 50000.0000
min 18.0000 35000.0000
max 47.0000 80000.0000
std 9.0554 17888.5438
Different Functions per Column
This is where .agg() really shines — assign different aggregations to different columns using a dictionary.
custom_summary = df.agg({
'Age': ['min', 'max', 'mean'],
'Salary': ['sum', 'mean', 'median']
})
print(custom_summary)
With groupby(): The Real Power
Combine .agg() with groupby() for multi-level summaries.
# Add a department column for grouping
df['Department'] = ['Sales', 'Engineering', 'Sales', 'HR', 'Engineering']
# Group by department and apply different stats
dept_summary = df.groupby('Department').agg({
'Age': 'mean',
'Salary': ['sum', 'mean', 'count']
})
print(dept_summary)
Custom Aggregation Functions
You can pass your own functions — great for domain-specific metrics.
# Custom function: coefficient of variation (std / mean)
def cv(x):
return x.std() / x.mean() if x.mean() != 0 else 0
# Apply custom + built-in
advanced = df[['Age', 'Salary']].agg({
'Age': ['mean', cv],
'Salary': ['mean', 'sum', cv]
})
print(advanced)
Named Aggregations (Clean Column Names)
Use named tuples for readable output (Python 3.6+).
from pandas import NamedAgg
named_summary = df.agg(
age_mean=('Age', 'mean'),
age_range=('Age', lambda x: x.max() - x.min()),
salary_total=('Salary', 'sum'),
salary_cv=('Salary', cv)
)
print(named_summary)
Modern Alternative in 2026: Polars
For large datasets, Polars is often faster and more memory-efficient.
import polars as pl
df_pl = pl.DataFrame(data)
summary_pl = df_pl.select([
pl.col("Age").mean().alias("age_mean"),
pl.col("Salary").sum().alias("salary_total")
])
print(summary_pl)
Best Practices & Common Pitfalls
- Use dictionary syntax for different functions per column — much clearer
- Avoid mixing list and dict syntax — stick to one style
- Reset index after groupby.agg() if you want a flat DataFrame
- Handle missing data before aggregation (fillna or dropna)
- For huge data, consider Polars or chunked processing
Conclusion
The .agg() method is one of Pandas' most powerful features — it turns complex multi-function aggregations into clean, readable code. In 2026, master .agg() with groupby() and custom functions, and you'll write faster, clearer data summaries that scale from quick exploration to production reporting.
Next time you need multiple statistics — reach for .agg() first.