Different Statistics in a Pivot Table – Advanced pivot_table() in Pandas 2026
One of the most powerful features of pivot_table() is the ability to apply different aggregation functions to different columns or even multiple functions to the same column. In 2026, this capability makes pivot_table() the go-to tool for creating rich, multi-metric summary reports.
TL;DR — How to Use Different Statistics
- Use a dictionary in
aggfuncto assign different functions per column - Use a list of functions for multiple statistics on the same column
- Combine with
indexandcolumnsfor multi-dimensional views
1. Different Statistics for Different Columns
import pandas as pd
df = pd.read_csv("sales_data.csv", parse_dates=["order_date"])
pivot = pd.pivot_table(
df,
values=["amount", "quantity"],
index="region",
columns=df["order_date"].dt.to_period("M").rename("month"),
aggfunc={
"amount": ["sum", "mean"], # two stats for amount
"quantity": "sum" # only sum for quantity
},
margins=True
).round(2)
print(pivot)
2. Multiple Statistics on the Same Column
sales_pivot = pd.pivot_table(
df,
values="amount",
index=["region", "category"],
columns=df["order_date"].dt.year.rename("year"),
aggfunc=["sum", "mean", "count", "std"],
margins=True
).round(2)
print(sales_pivot)
3. Advanced Example – Mixed Aggregations
report = pd.pivot_table(
df,
values=["amount", "quantity", "customer_id"],
index="region",
columns="category",
aggfunc={
"amount": ["sum", "mean", "max"],
"quantity": "sum",
"customer_id": "nunique"
},
margins=True,
margins_name="Total"
).round(2)
print(report)
4. Best Practices in 2026
- Use a **dictionary** in
aggfuncwhen you need different statistics per column - Use a **list** of functions when you want multiple stats on the same column
- Always include
margins=Truefor grand totals when creating reports - Round results for clean, professional-looking output
- Combine with date components extracted via
.dtfor time-based pivots
Conclusion
Applying different statistics within a single pivot table is one of the most useful capabilities in Pandas. In 2026, using pivot_table() with a dictionary in aggfunc allows you to create comprehensive, multi-metric reports in just a few lines of code. This technique is perfect for management dashboards, monthly reports, and any situation where you need to see multiple perspectives of your data at once.
Next steps:
- Create a pivot table that shows both total sales and average order value by region and category