Skip to content

Pivot and unpivot

Move data between long and wide, and know which shape each analysis needs.

Applied ~10 min8 steps

What runs in CodeSplash

You will be able to

  • Tell long from wide tables
  • Predict the row count after reshaping
  • Choose the shape a repeated-measures analysis needs

Read first

01

Two shapes of the same numbers

Read

Wide:

idbeforeafter
A57

Long:

idtimescore
Abefore5
Aafter7

WideLong vs wide dataWide data has one row per subject and one column per measurement; long data has one row per measurement. has one row per subject, and longLong vs wide dataWide data has one row per subject and one column per measurement; long data has one row per measurement. has one row per measurement. Neither is wrong. Wide reads well on a page. Long makes time a variable you can group by, filter and model.

02

How many rows?

Predict

Predict first

Ten patients each have columns for three visits: visit_1, visit_2 and visit_3. You unpivot those columns. How many rows do you get?

Make your guess between 1 and 100 rows, then reveal.

Reveal

Thirty: one row per patient per visit, or 10×310 \times 3. Check this count after any reshape. If it doesn't match, your keys aren't what you thought.

03

In Prepare

In the app

In Prepare (⌘3):

  • Unpivot columns to rows stacks columns into a name column and a value column (wide to long).
  • Pivot to columns spreads a column's values into new columns (long to wide), with one row per key combination.

Name the new columns for what they mean, such as visit and score, not name and value.

04

Which shape does the test want?

Explore

Open each of these and look at the roles it asks for. The roles tell you the shape.

When a dialog asks for a subject column and a condition column, it wants long data. When it asks for two measurement columns, it wants wide data.

05

When pivoting collides

Read

Pivoting wide needs one value per key combination. If patient A has two rows for visit_2, something must give: the row was entered twice, or the key needs another column such as date.

06

In code

Read

The Code panel on a recipe writes this step for you. By hand it looks like this:

Python
import pandas as pd

wide = pd.DataFrame({"id": ["A", "B"], "visit_1": [5, 6], "visit_2": [7, 8]})
long = wide.melt(id_vars="id", var_name="visit", value_name="score")
back = long.pivot(index="id", columns="visit", values="score").reset_index()
R
library(tidyr)

wide <- data.frame(id = c("A", "B"), visit_1 = c(5, 6), visit_2 = c(7, 8))
long <- pivot_longer(wide, starts_with("visit"), names_to = "visit", values_to = "score")
back <- pivot_wider(long, names_from = visit, values_from = score)
07

Check your understanding

Checkpoint

Check yourself

A table has 12 stores and a sales column for each of 5 years. How many rows are there after you unpivot the year columns?

Show the answer

Answer: 60

One row per store per year: 12 × 5.

Check yourself

You want to chart a score over visits, with one line per patient. Which shape do you need?

Show the answer

Answer: a

A chart maps columns to axes. The visit has to be a column before it can go on an axis.

08

Recap

Recap
  • Wide has one row per subject, and long has one row per measurement.
  • Predict the row count, then check it.
  • The roles in an analysis dialog tell you which shape it wants.

Concepts in this lesson