pandas basics · lesson 9 of 9
Parts in this lesson
Apd.read_csv(RAW / name)3.3: the raw file, never edited
Bpd.to_datetime(s, format="%d/%m/%Y")dates in a format you name
Cdf.drop_duplicates()rows that appear twice
Ddf.to_csv(CLEAN / name, index=False)3.3: the clean copy

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.replace and .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:

  1. Store names: Lipa City is the Lipa store.
  2. Dates: one date, 04/01/2024, is written day/month/year.
  3. Missing kilos: Tanauan on 3 January.
  4. Impossible kilos: 3100 at Tanauan on 4 January, probably 31.00 with the point lost.
  5. A repeated row: Lipa on 4 January, twice.
  6. Prices in two formats: ₱45.00 and 45.

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 to data/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.

02-clean.ipynb
  1. Tindahan: clean January's rice sales

    Fixes the six problems found in 01-first-look.ipynb and 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:

  • date is str, not dates, because one value isn't in the same format.
  • kilos is float64, not whole numbers, because of the gap (lesson 3.7). The NaN is in pink in the table.
  • price_per_kilo is str, 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.

02-clean.ipynb
  1. Store names. Lipa City is the Lipa store.

  2. Dates. One date is day/month/year. Read each format on purpose, and fail loudly on anything else.

  3. Prices. Remove the peso sign and make them numbers.

  4. Kilos. 3100 at Tanauan is 31.00 with the point lost.

  5. Duplicates. Lipa on 4 January is in the file twice.

  6. Missing kilos. Tanauan on 3 January has none, and nobody knows what it sold. Dropped, not filled with a guess.

  • Store names. replace turns Lipa City into Lipa. value_counts now 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-02 style and once as 04/01/2024 style, with errors="coerce" so each attempt gives NaT (a missing date) where it doesn't fit. fillna takes 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 a dayfirst=True option, 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.00 and 45 become 45.0.
  • Kilos. .loc with 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, and drop_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.

02-clean.ipynb
  1. Tindahan: clean January's rice sales

    Fixes the six problems found in 01-first-look.ipynb and saves a clean copy.

    In: data/raw/sales_2024-01.csv, never edited. Out: data/clean/sales_2024-01.csv.

  2. Fixes, one per problem found in the first look. Tanauan's missing day is dropped, not guessed.

  3. Checks. Each line should print True.

  4. Save the clean copy, and read it back to be sure.

  5. 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_csv and dtypes (3.2 and 3.3) to load the file and see what was wrong.
  • replace, str.replace, to_datetime and .loc (3.6 and 3.7) to fix values.
  • drop_duplicates and dropna(subset=...) (3.8), each with a reason.
  • Checks in code, and to_csv(index=False) into data/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.

All sources for Simple Data Analysis