There is a quiet law behind almost every data-science tool you will ever use: they all expect your data in the same shape. Learn that shape once and every plotting library, every model, every join clicks into place. Ignore it and you will spend your career reshaping spreadsheets by hand. Hadley Wickham gave the shape a name in a now-famous 2014 paper — tidy data — and borrowed a line from Tolstoy to describe it: "tidy datasets are all alike; every messy dataset is messy in its own way."
A tidy table obeys three rules, and they are gloriously simple.
That is the whole idea. A "variable" is something you measure (height, temperature, price); an "observation" is one thing you measured it on (a person, a day, a transaction). When rows are observations and columns are variables, the data lines up perfectly with how every statistical model thinks — which is no accident, because the models came first and the convention grew up to feed them.
Here is a common mess: a table where the column headers are values, not variable names. Three patients, their blood pressure measured in two different years, crammed into a "wide" layout:
| patient | 2023 | 2024 |
|---|---|---|
| Ada | 120 | 124 |
| Bo | 135 | 130 |
| Cai | 118 | 122 |
The problem: year is a variable, but it is hiding in the headers 2023
and 2024, and blood pressure is a variable smeared across two columns.
Tidied, one row per observation (a patient in a year), it becomes:
| patient | year | bp |
|---|---|---|
| Ada | 2023 | 120 |
| Ada | 2024 | 124 |
| Bo | 2023 | 135 |
| Bo | 2024 | 130 |
| Cai | 2023 | 118 |
| Cai | 2024 | 122 |
The first table is wide, the second is long. Reshaping between them — pivoting and melting — is one of the most common moves in all of data wrangling. Wide is nice for a human to read; long (tidy) is what your tools want to eat.
Once data is tidy, it is a short step to the object every model actually consumes: the
data matrix (or design matrix). Stack
Row
This is why tidy data is not mere tidiness for its own sake. "One observation per row, one variable
per column" is the definition of
The name is a fossil from the era of designed experiments. When R. A. Fisher and his successors laid
out agricultural trials — this fertiliser on that plot — the matrix of which treatments applied to
which plot was literally the design of the experiment. The rows were plots, the columns were
the experimental factors, and the matrix encoded the plan. Modern machine learning inherited the
object and the word wholesale, even though our "designs" are now scraped from the wild rather than
planted in a field. When you hear "design matrix," just think: the tidy table, written as
A tempting mistake, especially for anyone coming from spreadsheets, is to lay data out
transposed — one row per variable and one column per observation, so the table grows
sideways as you collect more data. It reads fine to a human but breaks every tool: models,
groupby operations and plotting libraries all assume n grows downwards, one new
row per new observation, with a fixed set of named columns. If adding another data point means adding
a column, you have the table on its side. Rows are the things you observed; columns are what
you measured about them — never the reverse.
Reshaping between wide and long, and turning a raw file into a clean design matrix, is the daily bread
of the Pandas course
on Kaggle Learn — its melt, pivot and indexing lessons are the tidy-data
rules in code. Work through them and the matrix