pbPassingBI
/
Aggregation & reshaping intermediate 6 min

Pivoting and reshaping

pivot_table and melt — moving between wide and long.

What you'll be able to do
  • Pivot long data into wide
  • Melt wide data into long
  • Explain why long form suits analysis

The two shapes

wide product | Jan | Feb A 100 120 B 90 140 melt pivot long product | month | value A Jan 100 A Feb 120 B Jan 90 charts and groupby want long data
Most reshaping work is moving between these two forms.

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.

Key points
  • pivot_table aggregates duplicates; pivot raises on them
  • melt is the unpivot — the fix for spreadsheet-shaped data
  • Long form is what groupby and plotting libraries expect
Check yourself