Start studying
Module 6 · Data Wrangling Module contents
Module 6  ·  Card 13 / 14intermediate

Pivoting Long to Wide

Pivoting turns a long table, with one row per measurement, into a wide one with a column per category.

You pick a column for the new rows, a column whose values become the new headers, and a column of values to fill the grid.

For example, a table of store, month and revenue pivots into one row per store and one column per month.

The pivot method needs each row and column pair to appear once, while pivot_table combines repeats with a sum or mean.

Wide tables are easier to read and chart, while long tables are easier to filter, group and store.

Learn in depth ↓
Module 06 · Data Wrangling · 14 concepts
In depth  /  15 lines

Pivoting Long to Wide, in more detail

The same data can be laid out long or wide, and different jobs need different layouts.

Long data has one row per observation, such as one row for each store in each month.

Wide data spreads one variable across several columns, such as a column for each month.

Tidy data from module 01 is usually long, which suits filtering and grouping.

Reports, charts and some models want wide data, with one row per item and its values side by side.

The pivot method only reshapes, so every cell of the new grid must come from exactly one row.

The pivot_table function summarises first, so repeated pairs are combined with a sum, a mean or another calculation.

Pairs that never occur become blank cells, which fill_value can set to 0 where 0 is the honest answer.

Using several columns for the rows, such as region and store, gives layered row labels.

Wide tables grow with the data: a new month adds a new column, which breaks code that expects fixed columns.

That is one reason data is usually stored long and pivoted only for display.

The unstack method does the same reshaping from a layered index, and stack reverses it.

The spreadsheet pivot table from earlier in this module and pivot_table in pandas are the same idea in two tools.

In practice pivoting is often the last step before a chart or a report table.

The common confusion is between pivot, which only reshapes, and pivot_table, which also summarises.

Want the full technical article? Wikipedia ↗
Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
An analyst pivots a sales table with store as rows and month as columns, and gets an error about duplicate entries. Why?
Data Wrangling

14 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close