pythonbeginner
Pandas Pivot Table Summary
Create multi-level summary pivot tables from transactional data using pd.pivot_table.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
import numpy as np
df = pd.DataFrame({
'region': np.random.choice(['North','South','East','West'], 200),
'product':np.random.choice(['A','B','C'], 200),
'channel':np.random.choice(['online','store'], 200),
'sales': np.random.randint(10, 500, 200),
})
table = pd.pivot_table(
df,
values='sales',
index=['region','product'],
columns='channel',
aggfunc={'sales': ['sum','mean']},
fill_value=0,
margins=True,
)
print(table)Use Cases
- sales reporting
- cross-tab analysis
- BI summaries
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
Pandas GroupBy Aggregation Examples
GroupBy operations with multiple aggregations, named aggregations, and transform for DataFrame analysis.
Best for: Sales reporting by region and time period
#pandas#groupby
pythonbeginner
Pandas Time-Series Resampling
Resample time-series data from daily to weekly/monthly frequencies with aggregation functions.
Best for: time-series analytics
#pandas#time-series
pythonintermediate
Pandas Custom Aggregation Functions
Pass custom lambda and named functions to .agg() for complex groupby aggregations.
Best for: HR analytics
#pandas#groupby
pythonbeginner
Pandas Styled DataFrame Report
Apply conditional formatting to a pandas DataFrame for styled HTML reports with highlighting.
Best for: executive reporting
#pandas#styling