Project: the Tindahan notebook
Last updated
Time to put Stage 1 together. January's rice sales have arrived from three Tindahan stores, as a raw file nobody has checked. You'll set up the project, open the file in a notebook, hunt for problems with plain Python, and write down what you find, ready for the cleaning in Stage 3.
By the end of this lesson you can
- Set up a project folder with raw data kept apart from everything else
- Open a CSV with
pathlibandcsv, and look at what it holds - Check a file for misspellings, odd dates, missing and impossible values, duplicates and mixed formats
- Write a notebook that reads like a report and passes Restart and run all
Plan it
The stores have sent one file, sales_2024-01.csv: a row for each store, each day, with the kilos of regular rice sold and the price per kilo. Before anyone works out totals or draws charts, someone has to answer a plainer question:
What's in this file, and is anything wrong with it?
That's this project. You won't fix anything yet: fixing is Stage 3, with pandas. Today you look, and write down what you see. A good first look checks:
- Names. Is every store spelled one way?
- Dates. Is every date written the same way?
- Numbers. Is any missing? Is any far too big or too small to be true?
- Repeats. Is any row in the file twice?
- Formats. Is every price written the same way?
The tools are all from this stage: a project folder (1.5), a notebook (1.2), notes in Markdown (1.3), and pathlib and csv (1.5).
Set up the project
On your own computer, use the tindahan folder from lesson 1.4. Add the folders from lesson 1.5 and start JupyterLab (on a Mac, use / in place of \ and source .venv/bin/activate):
cd tindahan mkdir data\raw, data\clean, notebooks .venv\Scripts\activate (.venv) $ jupyter lab
Put sales_2024-01.csv in data/raw/, and make a notebook in notebooks/ called 01-first-look.ipynb. Remember that a notebook in notebooks/ reaches the data with Path("../data/raw").
This page's notebooks already have the file, in data/raw/, so you can do the whole project here too. Their paths start from the project folder, so they use Path("data/raw").
Open the raw file
Start the way every analysis starts: a heading that says what the notebook is for, then open the file and look. Run each cell.
-
Tindahan: first look at January's rice sales
What's in the raw file, and is anything wrong with it?
-
-
-
-
- 13 rows: three stores, a few days each. Small enough to read by eye, which makes it a good place to learn checks that will still work on 13 million rows.
rows[0]shows that every value is text, even the kilos:'42', with quotes.csvnever guesses types. Turning text into numbers is part of cleaning.- The keys are the column names from the file's first line.
Now look at the whole file with rows in a new cell, and see how many problems you can spot before the next step.
Hunt for problems
Reading by eye doesn't scale, so each check below is a few lines of code. One new trick does most of the work: {r["store"] for r in rows} is a set comprehension. Like a list comprehension, but a set keeps each value once, so it shows the different values in a column. sorted puts them in order.
Run the cells one at a time. Before each one, read the note and guess what it'll find.
-
-
Stores. There should be three: Batangas, Lipa and Tanauan.
-
-
Dates. Every date should look like
2024-01-02. -
-
Kilos. Every row needs a number, and a small store sells tens of kilos a day, not thousands.
-
-
Duplicates. The same row twice usually means a file was uploaded twice.
-
-
Prices. They should all be written the same way.
-
-
What each check found:
Lipa City: the same store asLipa, typed two ways. Counted as two stores, Lipa's sales would be split in half.04/01/2024: one date written day/month/year. Is it 4 January or April 1? From the rows around it, 4 January, but code can't tell that on its own.- A blank in kilos for Tanauan on 3 January. Not zero: nobody knows. Missing is different from none.
3100kilos at Tanauan on 4 January, in a store that sells about 30 a day. Probably31.00with the point lost, like the typo in lesson 1.1.- A repeated row: Lipa on 4 January appears twice. Added up as it is, it would count that day's sales twice.
45next to₱45.00: one price written without the peso sign and decimals.
None of these stopped the code with an error. Every one would quietly give a wrong total. That's why the first look matters.
Write what you found
The checks are only half the job. Someone reading the notebook (your manager, or you in Stage 3) needs to know what they mean. Under the last cell is a note that starts What I found. Click Edit note, and for each problem write one line: what it is, where it is, and what you think happened. For example:
- Lipa City (3 Jan) is the Lipa store, spelled differently.
When you're done, press Show it to see it formatted.
-
Tindahan: first look at January's rice sales
What's in the raw file, and is anything wrong with it?
Data:
data/raw/sales_2024-01.csv, as the stores sent it. Not edited. -
-
Stores. There should be three: Batangas, Lipa and Tanauan.
-
-
Dates. Every date should look like
2024-01-02. -
-
Kilos. Every row needs a number, and a small store sells tens of kilos a day, not thousands.
-
-
Duplicates. The same row twice usually means a file was uploaded twice.
-
-
Prices. They should all be written the same way.
-
-
-
What I found
-
The finished notebook
Last check, the one from lesson 1.2: press Restart and run all on the notebook above. Memory empties, every cell runs from the top, and the counts go In [1], In [2], In [3]… in order. If it still says Problems found: 6, the page means what it says.
Look at what you've made:
- A heading and a question at the top, and where the data came from.
- Imports and the file in the first code cell.
- One check per cell, each with a note saying what it looks for.
- A summary in words at the end.
- And
data/raw/sales_2024-01.csvexactly as it arrived.
In Stage 3 you'll open the same file with one line of pandas and fix each problem on your list, in code, writing a clean file to data/clean/. The list you just wrote is the plan for that work.
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.
- The Python Standard Library: csv: DictReader, and opening with newline=''
- The Python Tutorial: Sets: a set keeps each value once
- Broman and Woo, Data organization in spreadsheets (The American Statistician, 2018): be consistent: one way to write names, dates and missing values
- Wickham, Tidy Data, Journal of Statistical Software 59(10), 2014: the kinds of mess real data arrives with