Multiple Statistics in a Pivot Table – Advanced pivot_table() Techniques 2026
Creating pivot tables with multiple statistics (sum, mean, count, std, etc.) on the same or different columns is a very common requirement for professional reports. In 2026, Pandas pivot_table() makes this elegant and flexible using dictionaries and lists inside the aggfunc parameter.
TL;DR — How to Apply Multiple Statistics
- Use a **dictionary** to assign different functions to different columns
- Use a **list** to apply multiple functions to the same column
- Combine both approaches for rich multi-metric pivot tables
- Use
margins=Trueto add grand totals
1. Multiple Statistics on 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", "max"], # multiple stats for amount
"quantity": ["sum", "mean"] # multiple stats 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", "min"], # multiple functions on amount
margins=True
).round(2)
print(sales_pivot)
3. Combined Approach – Rich Multi-Metric Pivot Table
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="Grand Total"
).round(2)
print(report)
4. Best Practices in 2026
- Use a dictionary in
aggfuncwhen applying different statistics to different columns - Use a list when you want several statistics on the same column
- Always round the final pivot table for clean reports
- Add
margins=Trueto include grand totals - Use meaningful index and column names (including date components via
.dt)
Conclusion
Applying multiple statistics within a single pivot table is one of the most useful reporting techniques in Pandas. In 2026, using pivot_table() with a combination of dictionary and list in aggfunc allows you to create comprehensive, multi-dimensional reports that show totals, averages, counts, and more — all in one clean table. This approach is widely used for management dashboards and business intelligence reports.
Next steps:
- Create a pivot table that shows both total sales and average order value by region and category using multiple statistics