The two shapes
pivot_table
pd.pivot_table(df,
index='region',
columns='year',
values='revenue',
aggfunc='sum',
fill_value=0,
margins=True) # adds row and column totals
This is the Excel pivot table, in one call. aggfunc accepts a list — ['sum', 'mean'] — for several measures at once.
pivot_table aggregates duplicates; the plain pivot method raises on them. Prefer pivot_table unless you specifically want that error.
melt
pd.melt(df,
id_vars=['product'],
value_vars=['jan', 'feb', 'mar'],
var_name='month',
value_name='revenue')
The unpivot. A spreadsheet with one column per month becomes rows of month and value — which is what you need before grouping or charting by month.
This is the single most useful reshaping operation when working with data that came from a spreadsheet.
crosstab
pd.crosstab(df['region'], df['status'])
pd.crosstab(df['region'], df['status'], normalize='index') # row percentages
A frequency table of two columns. normalize turns counts into proportions, which is usually what you actually wanted.
stack and unstack
df.stack() # columns into index rows
df.unstack() # index level back into columns
Mostly used to tidy up the multi-level result of a grouped aggregation.