The .agg() Method in Pandas – Powerful Aggregations in Python 2026
The .agg() (aggregate) method is one of the most flexible and powerful tools in Pandas for performing custom summary statistics. In 2026, using .agg() with method chaining has become the standard way to create clean, readable, and efficient aggregation pipelines.
TL;DR — Why .agg() is Essential
- Apply multiple aggregation functions at once
- Use different functions for different columns
- Works beautifully with
groupby() - Supports custom functions and named outputs
1. Basic Usage of .agg()
import pandas as pd
df = pd.read_csv("sales_data.csv", parse_dates=["order_date"])
summary = df.agg({
"amount": ["count", "sum", "mean", "median", "min", "max", "std"],
"quantity": ["sum", "mean"],
"customer_id": "nunique"
}).round(2)
print(summary)
2. Combined with groupby() – Most Common Pattern
result = (
df
.groupby(["region", df["order_date"].dt.to_period("M")])
.agg({
"amount": ["sum", "mean", "count"],
"quantity": "sum",
"customer_id": "nunique"
})
.round(2)
)
# Rename columns for cleaner output
result.columns = ["total_sales", "avg_sale", "order_count", "total_quantity", "unique_customers"]
print(result)
3. Advanced .agg() with Named Outputs and Custom Functions
def range_func(x):
return x.max() - x.min()
summary = (
df
.groupby("region")
.agg(
total_sales=("amount", "sum"),
avg_sale=("amount", "mean"),
sales_range=("amount", range_func),
unique_customers=("customer_id", "nunique")
)
.round(2)
)
4. Best Practices in 2026
- Use dictionary syntax with column → list of functions for clarity
- Use named aggregation syntax (
new_name=("column", "func")) for clean column names - Combine
.agg()withgroupby()and method chaining - Define small custom functions when built-in ones are not enough
- Always round final results for readability
Conclusion
The .agg() method is the modern, Pythonic way to perform powerful aggregations in Pandas. In 2026, mastering .agg() together with groupby() and method chaining allows you to write concise, readable, and highly efficient data summarization code that scales from small datasets to millions of rows.
Next steps:
- Replace your multiple separate
.sum(),.mean()calls with a single, well-structured.agg()operation