Pivot and unpivot
Move data between long and wide, and know which shape each analysis needs.
What runs in CodeSplash
- Paired t-test Verified in Python and R
- Repeated-measures ANOVA (one within factor) Verified in Python and R
You will be able to
- Tell long from wide tables
- Predict the row count after reshaping
- Choose the shape a repeated-measures analysis needs
Two shapes of the same numbers
ReadWide:
id | before | after |
|---|---|---|
| A | 5 | 7 |
Long:
id | time | score |
|---|---|---|
| A | before | 5 |
| A | after | 7 |
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.
How many rows?
PredictPredict 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 . Check this count after any reshape. If it doesn't match, your keys aren't what you thought.
In Prepare
In the appIn 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.
Which shape does the test want?
ExploreOpen 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.
When pivoting collides
ReadPivoting 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.
In code
ReadThe Code panel on a recipe writes this step for you. By hand it looks like this:
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()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)Check your understanding
CheckpointCheck 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.
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