Getting and cleaning data · lesson 5 of 11
Parts in this lesson
Adf.melt(id_vars="store", var_name="month")column headers into a column
Bs.str.split("-", expand=True)one column into two
Cdf.pivot(index=..., columns="measure")rows of variables into columns

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.split or pivot

Three rules

A table is tidy when:

  1. Each variable is a column. Date, store, item and kilos each have one column.
  2. Each observation is a row. One sale of one item, at one store, on one day.
  3. 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.

monthly.ipynb

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.

tidy.ipynb
  • Headers that are values → melt. id_vars="store" keeps the store column; the month names become a month column, and the numbers a kilos column. 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 a groupby (Batangas and Tanauan sold most in March, Lipa in February). April is just more rows.
  • Several variables in one column → str.split. Lipa-rice holds a store and an item. expand=True gives 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. pivot gives each its own (lesson 4.4).

Sort the tables

For each table, pick the fix that makes it tidy.

0 of 6 placed right

  1. Columns store, 2022, 2023, 2024, holding kilos

  2. A column town_province holding Lipa_Batangas

  3. Columns date, store, reading, value, where reading is rain_mm or temp_c

  4. One table of sales where every row repeats the store’s address and opening year

  5. Columns date, store, item, kilos: one row per store, day and item

  6. Columns store, rice_kg, sugar_kg, oil_kg

Check what you learned

Which table is tidy?
A table has columns store, 2023 and 2024. What makes it tidy?

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.

All sources for Simple Data Analysis