Getting and cleaning data · lesson 11 of 11
Parts in this lesson
Apd.read_csv(path, skiprows=2)5.2: a sheet’s table
Bpd.json_normalize(page["results"])5.4: JSON into columns
Cpd.read_sql(query, con)5.3: SQL into a DataFrame
Dcheck(**tables)5.9: every rule, every time

Project: clean all of Tindahan

Last updated

Project 4.P started from four clean files. In this project you'll make them, from five raw files in four formats: two months of sales CSVs, a spreadsheet of stores, a price list from an API, and a weather database. One function per file, checks across all four tables, a cleaning log in Markdown, and one command that rebuilds everything from raw.

By the end of this lesson you can

  • Write one cleaning function per raw file, from CSV, spreadsheet, JSON and SQL
  • Check the clean tables against each other, not just one at a time
  • Write a cleaning log in Markdown from the code
  • Rebuild every clean file from raw with one command

Plan it

Five raw files have arrived for January and February 2024. The data is made up for this course, the same simulated data as project 4.P, but it arrives the way real data does:

raw filefromproblems to expect
sales_2024-01.csvthe stores' spreadsheetstore names typed differently; repeated rows
sales_2024-02.csvthe stores' spreadsheetdates day/month/year; one store weighs in grams; repeated rows
stores.csvthe head office's sheettitle rows, a blank row and a total
products.jsonthe price list APInested prices, in centavos; item names capitalised
weather.sqlthe weather office's databasestation codes instead of towns

The goal: the four clean files project 4.P used, sales.csv, stores.csv, products.csv and weather.csv, made by one script, checked, with a log. The rules from lesson 5.10 apply: raw files are only read, every fix is a function, and every function counts what it changed.

Look at the raw files

Always look before you clean. Run the cells.

04-clean-all.ipynb
  1. Tindahan: clean every raw file

    Turns the five raw files into the four clean tables used in 03-report.ipynb, with checks and a log.

Three lines of February's sales show the day-first dates. The stores sheet starts with two title rows before its header. The weather file is SQL: it makes two tables, stations and readings, and fills them. Each format needs the tool from its lesson: skiprows (5.2), json_normalize (5.4), and sqlite3 with read_sql (5.3).

The last cell prints products.json: prices are in centavos, inside a price dictionary, and the item names start with capitals.

One function per file

Each function reads one raw file, fixes it, logs each fix, and returns a clean table. Run the cells in order.

04-clean-all.ipynb
  • clean_sales reads both months, each with its own date format named on purpose (lesson 4.6), and stacks them with concat. Store names are tidied with strip, title and one replace; Rosario's February grams are divided by 1,000 using the unit column (lesson 5.8); exact repeats are dropped. 720 rows: 60 days, 4 stores, 3 items.
  • clean_stores skips the title rows, makes the column names lowercase, and keeps only rows that name a store and aren't the total.
  • clean_products flattens the JSON and turns centavos into pesos.
  • clean_weather runs the SQL file into a database in memory, then one query joins each reading to its station's town and sorts them. 240 rows: 60 days in 4 towns, from 1 January to 29 February.

The log has six lines. 53 store names were tidied, 87 Rosario rows were in grams (29 days, 3 items), and 5 repeats were dropped.

Checks across the tables

Each table can look fine on its own and still not fit the others: a store in the sales that isn't in the stores table, or a town with no weather. These checks look across all four. Run the cells.

04-clean-all.ipynb

The clean tables pass every check: []. The second cell breaks a copy on purpose, a negative kilos value and a store called Nasugbu, and check names both problems. Test every check like this once, by breaking the data and seeing it fail. A check that has never failed might not work.

The cross-table checks are the ones that protect project 4.P's joins: every sale's store is in stores, every item has a price, and every town has a reading for every day. Those are exactly what how="left" with no NaN showed there.

Rerun from raw

The functions, the checks and a main() are now in src/tindahan.py, with a tiny clean_all.py to run it. main() cleans all four, checks them together, and only then writes the clean files and the log. If any check fails, it raises an error and writes nothing, so a half-cleaned folder can't happen. Run it.

04-clean-all.ipynb

On your computer, from the project folder:

Terminal, in the tindahan folder
python clean_all.py
sales: 720 rows
stores: 4 rows
products: 3 rows
weather: 240 rows
All checks passed

The log, data/clean/cleaning_log.md, is a Markdown table that the code writes on every run: what changed, in which file, and how many rows. Anyone can open it and see exactly what was done to the raw data, and it can never be out of date.

The four clean files are the same, character for character, as the ones project 4.P started from. Delete data/clean/, run the command, and they're back.

Finished

Run the notebook in step 5 again from the top: it should print the four row counts and All checks passed, and the log should list six changes.

Look at what you used:

  • skiprows, json_normalize and sqlite3 with read_sql (5.2, 5.4 and 5.3) for three different formats.
  • Unit columns and tidied names (5.8), and repeats dropped (5.7).
  • Checks that look across tables, tested by breaking them (5.9).
  • Functions in src/, a log, and one command that rebuilds everything (5.10).

The Tindahan project now runs from raw files to report tables with nothing done by hand. When March's sales arrive, add the file and its date format to the list in clean_sales, rerun the command and the report notebook, and the report is up to date. In Stage 6 you'll turn its tables into charts.

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