Skip to content

Lesson 2 ยท Getting Started

A Safe Data Cleaning Workflow

Follow a safe data cleaning workflow: keep raw data untouched, make changes in code, log every rule, count rows before and after, and verify the result.

Cleaning is a chain of small decisions. If you make them by hand and forget them, nobody can check your work, including you next month. A good workflow makes each decision visible and repeatable.

This lesson gives you the habits that the rest of the tutorial relies on. They take little extra time, and they save a great deal of it when somebody asks why a number changed.

The six steps

Prepare

  1. State the goal
Which question?
  1. Protect raw data
Never edit it
1 / 3

Keep raw data untouched

Treat the original file or table as read-only evidence. Every cleaned version is built from it. If a rule turns out to be wrong, you fix the rule and run again. A simple folder layout is enough:

TEXT
project/
  raw/          original files, never edited
  work/         temporary outputs, safe to delete
  clean/        final outputs, rebuilt by your script
  clean.py      the rules
  NOTES.md      decisions and open questions

Also record where the raw data came from, when you received it and who can explain it. Raw does not mean correct. It means untouched.

Prefer rules to hand edits

Typing a correction into a cell leaves no trace. A rule such as "map NYC to New York" can be read, reviewed and applied again to next week's file. Even a one-off correction works well as a line in a small corrections table, with a reason beside it.

Count before and after

The cheapest safety check is counting. Count rows, unique IDs and missing values before and after each step. Unexpected changes show up immediately.

Python Log what each step changes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice two design choices. Each function returns a new list instead of changing its input. And the order of steps matters: tidying text first lets "oslo " and "Oslo" match later. Try swapping the two steps and watch the duplicate survive.

Flag before you delete

When you are not sure a row is wrong, add a column such as needs_review instead of removing it. You can leave flagged rows out of one analysis and still keep them for the owner to check. A flag with a reason, such as needs_review = "quantity looks high", is even more useful.

Keep a decision log

A log can be a short table. It answers the question "why does this number differ from the old report?"

Rule Reason Rows affected Approved by
Treat -999 as missing age Export system's placeholder 14 Data owner
Remove repeated order IDs File was loaded twice 220 Data owner

The values above are an invented example of the format.

Fix at the source when you can

If a form lets people type any country name, you will clean the same mess forever. Report the cause to whoever owns the system. A drop-down list, a required field or a database constraint prevents more errors than any script.

Common mistake: opening a CSV file in a spreadsheet, looking around and saving it. The program may rewrite long numbers, dates or leading zeros. Work on a copy and import with explicit column types.

Verify the result

Before you share a cleaned file, ask:

  1. Did the row count change only for reasons I can list?
  2. Do totals that should be stable still match the source?
  3. Are identifiers still unique where they must be?
  4. Did I check some changed rows by eye?
  5. Can someone else rebuild the output from the raw data?

The validation lesson turns many of these questions into automatic checks.

When to stop

Stop when the data is good enough for the stated question and the remaining problems are written down. Chasing the last rare error can cost more than it is worth. Say plainly what is still imperfect, so readers can judge the results.

An example loop

The steps look like a straight line, but real work circles back. Imagine that you profile an order file and write a rule that removes repeated order IDs. When you verify the result, the row count drops far more than you expected. You look at the removed rows and discover that each order can have several lines. Your key was wrong.

Because the raw file is untouched and the rules live in code, the repair is small: change the key, run again and compare. If you had deleted rows by hand in the only copy, you would be asking someone to send the file again.

Add a note to your decision log when this happens. "Order ID is not unique per row; use order ID plus line number" saves the next person from repeating your mistake.

Working with the people who own the data

Many cleaning questions have no technical answer. Is a quantity of zero a canceled line or a free sample? Does an empty region mean online sales? Collect these questions in a list, with two or three example rows each, and ask them together. Show what you plan to do if you hear nothing, but do not treat silence as approval for risky changes such as deleting records.

It also helps to agree early on what "done" means: which columns must be reliable, which problems may remain and who signs off.

Keep the first version simple

You do not need a framework on day one. A single script that reads the raw file, applies a few named functions, prints counts and writes the result is already repeatable. You can split it into modules and add tests as it grows.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why keep the raw data unchanged?

    The raw copy lets you rerun, audit and correct your rules. It may still contain errors.
  2. 2. You are unsure whether a row is an error. What is a safe choice?

    A flag preserves the information and lets someone who knows the data decide.
  3. 3. What is a cheap and useful check after a cleaning step?

    Counts quickly reveal accidental deletions or multiplied rows.

Summary

  • State the goal, protect raw data, profile, fix with rules, verify and document.
  • Write rules in code or recorded steps, not hand edits.
  • Count rows and keys around each step.
  • Flag doubtful rows instead of deleting them.
  • Report causes so errors stop at the source.

Next: Reading Messy Files

Your progress is saved in this browser. No account needed.