Project: clean one Tindahan file
Last updated
In project 1.P you found six problems in January's raw sales file, using plain Python. Now you'll fix them with pandas: load the file, correct the store names, dates, prices and kilos, decide what to do with the repeated and missing rows, check the result, and save a clean copy that the rest of the course can trust.
By the end of this lesson you can
- Load a raw file and read its dtypes to see what needs fixing
- Fix names, dates, prices and impossible values with
replace,to_datetime,str.replaceand.loc - Drop repeated and missing rows, and write down why
- Check a cleaned table with code, and save it to
data/clean
Plan it
Here's the list you wrote at the end of project 1.P. It's the plan for this one:
- Store names:
Lipa Cityis the Lipa store. - Dates: one date,
04/01/2024, is written day/month/year. - Missing kilos: Tanauan on 3 January.
- Impossible kilos: 3100 at Tanauan on 4 January, probably 31.00 with the point lost.
- A repeated row: Lipa on 4 January, twice.
- Prices in two formats:
₱45.00and45.
The rules from Stages 1 and 3 apply:
- The raw file in
data/raw/is never edited. The notebook reads it, fixes it in memory, and writes a new file todata/clean/. - Every fix is code, so it reruns on February's file.
- Every decision that isn't obvious (dropping a row, changing a number) gets a note saying why.
You'll make a new notebook, 02-clean.ipynb, next to 01-first-look.ipynb.
Load it and read the types
One line loads what took several in Stage 1. Run the cells.
-
Tindahan: clean January's rice sales
Fixes the six problems found in
01-first-look.ipynband saves a clean copy.In:
data/raw/sales_2024-01.csv, never edited. Out:data/clean/sales_2024-01.csv. -
-
The table shows most of the problems at a glance, and dtypes shows the rest:
dateisstr, not dates, because one value isn't in the same format.kilosisfloat64, not whole numbers, because of the gap (lesson 3.7). TheNaNis in pink in the table.price_per_kiloisstr, because of the ₱.
Each wrong type is a problem to fix. Once they're all right, the data is mostly clean.
Fix each problem
One fix per cell, each under a note. Run them in order, and read each answer before you go on.
-
Store names.
Lipa Cityis the Lipa store. -
-
Dates. One date is day/month/year. Read each format on purpose, and fail loudly on anything else.
-
-
Prices. Remove the peso sign and make them numbers.
-
-
Kilos. 3100 at Tanauan is 31.00 with the point lost.
-
-
Duplicates. Lipa on 4 January is in the file twice.
-
-
Missing kilos. Tanauan on 3 January has none, and nobody knows what it sold. Dropped, not filled with a guess.
-
- Store names.
replaceturnsLipa CityintoLipa.value_countsnow shows three stores. Lipa has 5 rows, one more than the others, which is the repeat, still to come. - Dates. The cell reads every date twice, once as
2024-01-02style and once as04/01/2024style, witherrors="coerce"so each attempt givesNaT(a missing date) where it doesn't fit.fillnatakes the first answer and fills its gaps from the second. Then it checks: no date is missing, and they run from 2 to 5 January. A date in some third format would show up here as a gap, instead of being guessed. (There's adayfirst=Trueoption, but it leaves pandas to guess each date's format, and with dates, guesses go wrong quietly.) - Prices. Remove the ₱ and convert to
float. Both₱45.00and45become45.0. - Kilos.
.locwith a mask changes only the rows over 500 kg, dividing by 100. That's a judgement: the note above the cell says why. The biggest day is now 51 kg, which is believable. - Repeats.
duplicated()finds one row that's an exact copy of an earlier one, anddrop_duplicates()removes it: 12 rows. - Missing kilos. Nobody knows what Tanauan sold on 3 January, so the row is dropped rather than filled with a guess (lesson 3.8), and the note says so. With no gaps left, kilos can be whole numbers again:
astype("int64"). 11 rows.
Check, and save
Here's the finished notebook in one piece. It loads, fixes, checks with code and saves. Run all.
-
Tindahan: clean January's rice sales
Fixes the six problems found in
01-first-look.ipynband saves a clean copy.In:
data/raw/sales_2024-01.csv, never edited. Out:data/clean/sales_2024-01.csv. -
-
Fixes, one per problem found in the first look. Tanauan's missing day is dropped, not guessed.
-
-
Checks. Each line should print
True. -
-
-
Save the clean copy, and read it back to be sure.
-
-
What I changed
-
The checks are the important part. Each one tests a promise the clean file makes: three store names, every date a date, every kilos figure believable, no store twice on one day, no gaps. If February's file has a new problem, one of these will print False instead of a wrong total going into a report.
The dtypes are right now: dates, text, whole kilos and decimal prices. The last cell saves the clean file, reads it back, and prints 11 rows and 401 kg of rice: Batangas 171, Lipa 136 and Tanauan 94.
Last, the note at the bottom, What I changed. Click Edit note and list each change in a line, with how many rows it touched, for example:
- Dropped Tanauan on 3 January: kilos missing, and no way to know the true figure.
Someone using data/clean/sales_2024-01.csv needs to know it's 11 rows, not 13, and why.
Finished
Press Restart and run all on the notebook in step 4. All five checks should print True, and it should end with Clean rows: 11.
Look at what you used:
read_csvanddtypes(3.2 and 3.3) to load the file and see what was wrong.replace,str.replace,to_datetimeand.loc(3.6 and 3.7) to fix values.drop_duplicatesanddropna(subset=...)(3.8), each with a reason.- Checks in code, and
to_csv(index=False)intodata/clean/(3.3).
The project folder now has a raw file, a first-look notebook that lists its problems, and a cleaning notebook that fixes them. Anyone can rerun both from the top and get the same clean file. In Stage 4 you'll use it to answer real questions: totals per store, per week, and how the stores compare.
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.
- pandas: pandas.to_datetime: format, and errors="coerce"
- pandas: pandas.DataFrame.drop_duplicates: which rows count as repeats
- The Python Standard Library: datetime, format codes: %Y, %m and %d
- Broman and Woo, Data organization in spreadsheets (The American Statistician, 2018): keep raw data raw, and write down every change
- Wickham, Tidy Data, Journal of Statistical Software 59(10), 2014: what a clean table looks like