Tidy data
Last updated
There are many ways to lay out the same data, and one of them makes analysis easy: tidy data. In this lesson you'll learn its three rules, recognise the common ways tables break them, and fix each with melt, split or pivot.
By the end of this lesson you can
- State the three rules of tidy data
- Recognise headers that are values, several variables in one column, and variables stored in rows
- Fix each with
melt,str.splitorpivot
Three rules
A table is tidy when:
- Each variable is a column. Date, store, item and kilos each have one column.
- Each observation is a row. One sale of one item, at one store, on one day.
- Each kind of thing is its own table. Sales in one table, stores in another.
Every pandas tool you've learned works best on tidy tables: a mask picks rows, groupby groups by a column, a chart puts a column on each axis. The Tindahan sales file is tidy, which is why Stage 4 was mostly short.
Tidy tables aren't always the best to read. People like wide tables, with months across the top, and a report should show them that way. Keep data tidy while you work, and reshape for the reader at the end (lesson 4.4).
Wrong way: months as columns
The head office sends monthly kilos per store, the way people like to read them. Find February's total. Run the cells.
KeyError: 'month'. There's no month column: the months are column names, so you can't filter, group or sort by them. Each question needs new code: February's total is wide["Feb"].sum(), but "the best month for each store" needs idxmax(axis=1), a trend needs the columns in the right order, and when April arrives, every line that lists the months has to change.
The months are values of a variable, stored in the header. That's the most common way tables break rule 1.
Fixing each mess
Run the cells.
- Headers that are values →
melt.id_vars="store"keeps the store column; the month names become amonthcolumn, and the numbers akiloscolumn. Nine rows, one per store and month. Now every question uses the usual tools: February is a mask (3,273 kg), and each store's best month is agroupby(Batangas and Tanauan sold most in March, Lipa in February). April is just more rows. - Several variables in one column →
str.split.Lipa-riceholds a store and an item.expand=Truegives one column per part, assigned to two new names. - Variables stored in rows →
pivot. Kilos and pesos are different variables, with different units, and shouldn't share a column.pivotgives each its own (lesson 4.4).
Sort the tables
For each table, pick the fix that makes it tidy.
0 of 6 placed right
Columns
store,2022,2023,2024, holding kilosA column
town_provinceholdingLipa_BatangasColumns
date,store,reading,value, where reading israin_mmortemp_cOne table of sales where every row repeats the store’s address and opening year
Columns
date,store,item,kilos: one row per store, day and itemColumns
store,rice_kg,sugar_kg,oil_kg
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Wickham, Tidy Data, Journal of Statistical Software 59(10), 2014: the three rules, and the five common messes
- pandas: Reshaping and pivot tables: melt, pivot, stack and unstack
- pandas: pandas.Series.str.split: expand=True for one column per part