Building a Reproducible Cleaning Pipeline
Turn cleaning steps into a rerunnable, idempotent pipeline with logs, tests and a data dictionary, and make honest, documented decisions about exclusions.
Welcome back!
Step 1 of 12Learn
The report nobody could rebuild
For two years, a monthly customer report was produced by one analyst. Each month she downloaded three exports, pasted them into a workbook, fixed region names with find-and-replace, deleted test accounts she recognized by name, sorted, filtered, removed duplicates by eye and copied the results into a template. Then she changed jobs.
Her successor followed her notes and produced a customer count 9% higher than the previous month. Was it real growth or a missed step? Nobody could tell, because the old process existed only in one person's habits. It took three weeks to reconstruct.
The cure is to treat cleaning as software: a script, or a series of scripts, that takes the raw files as input and produces the clean data as output, with no manual steps in between. Run it today, run it next month, run it on a colleague's laptop: same input, same output.
That property is called reproducibility, and it changes everything. Mistakes become fixable, because you can correct one step and rerun. Reviews become possible, because the logic is written down. New data is cheap, because the pipeline already knows every rule. This final lesson pulls the course together into a pipeline you can trust and defend.
Step 2 of 12Compare
Manual workbook versus scripted pipeline
| Aspect | Manual process | Scripted pipeline |
|---|---|---|
| Where the steps live | In one person's habits and notes | In code anyone can read and rerun |
| Next month's export | Redo every click by hand | Run the same script again |
| Raw data | Often overwritten by edits | Read-only; outputs are new files |
| Explaining a 9% jump | Guesswork | Compare cleaning logs between runs |
| When the analyst leaves | Weeks to reconstruct | Hand over the repository |
Step 3 of 12Learn
Principles of a good cleaning pipeline
A cleaning pipeline doesn't need to be fancy. A single Python script or notebook that runs top to bottom is enough to start, if it follows a few principles:
- Raw data is read-only. The pipeline reads raw files and writes new ones. It never modifies its inputs.
- One step, one job. Separate functions for loading, normalizing missing values, parsing types, standardizing text, deduplicating and validating. Small steps are easy to test and reorder.
- Explicit rules in data, not buried in code. Mapping tables, allowed values and thresholds live in small files or clearly named constants, where non-programmers can review them.
- Deterministic. The same input always gives the same output. Avoid steps that depend on the current time, random sampling without a fixed seed or the order rows happen to arrive in.
- Idempotent. Running a cleaning step on already clean data changes nothing. If trimming spaces twice or deduplicating twice gives a different result than once, something is wrong.
- Version controlled. Keep the code, mapping tables and rule files in a version control system such as Git, so every change is dated and explained.
A useful test of the whole pipeline: delete every output file, run it from scratch, and compare the result with yesterday's. If they differ and the raw data didn't change, you have found a hidden manual step or a nondeterministic one.
Step 4 of 12Practice · Put in order
Put these pipeline stages in a sensible order.
Drag the items, or use the arrow buttons, to put them in order.
- Remove duplicates on the cleaned key
- Parse numbers and dates
- Run validation rules and quarantine failures
- Write the clean output and the cleaning log
- Convert placeholder values to true missing values
- Load raw files with explicit column types
- Standardize text and categories
Step 5 of 12Practice · Code challenge · Python
Write an idempotent clean_record(row) for a dict with "name", "email" and "age". Return a new dict where: name has whitespace runs collapsed, ends stripped and is in title case; email is stripped and lowercased; age is converted to an int (it may arrive as an int or as a string with spaces), or None if it is None or blank. Running clean_record on its own output must give the same result.
clean_record({"name": " ana PETROV ", "email": " Ana@Mail.COM", "age": " 34 "})clean_record({"name": "Li Wei", "email": "li@x.org", "age": ""})clean_record(clean_record({"name": " bo kim", "email": "BO@K.IO ", "age": 51})) == clean_record({"name": " bo kim", "email": "BO@K.IO ", "age": 51})clean_record({"name": "x", "email": "y@z.q", "age": None})["age"]
Step 6 of 12Learn
The cleaning log and the data dictionary
Two documents make a pipeline understandable to everyone who uses its output.
The cleaning log records what each step did to the data on this run:
| step | rows in | rows out | notes |
|---|---|---|---|
| load | 25,000 | 25,000 | 3 files |
| normalize missing | 25,000 | 25,000 | 1,912 placeholders → missing |
| deduplicate | 25,000 | 23,750 | 1,250 duplicate emails merged |
| validate | 23,750 | 23,375 | 375 quarantined (dates 240, status 135) |
Generate it automatically from the pipeline, one line per step. Comparing logs month to month is a powerful monitor: if duplicates jump from 1,250 to 6,000, something changed upstream.
The data dictionary describes the clean output: each column's name, meaning, type, unit, allowed values, how missing values are represented and how the column was derived. For example: "income_usd: annual household income in US dollars, integer; missing for 6% of rows; values above the 99th percentile capped at $480,000; imputed values flagged in income_imputed."
Together they answer the questions every reader eventually asks: where did these numbers come from, what was removed and what does this column really mean?
Step 7 of 12Practice · Work it out
Using the cleaning log pattern: a raw file has 25,000 rows. Deduplication removes 1,250 rows, and validation quarantines 375 of the remaining rows. How many rows reach the clean output?
Step 8 of 12Note
Step 9 of 12Learn
Honest cleaning
Cleaning gives analysts a lot of quiet power. Every decision to drop, cap, impute or merge moves the results, and readers rarely see those decisions. That power comes with responsibilities.
Don't clean toward the answer you want. Removing "outlier" stores because they make a new program look bad, or keeping them because they make it look good, turns cleaning into manipulation. Decide your rules before looking at the outcome, ideally before the analysis starts, and apply them the same way to every group.
Report what you excluded. "We analyzed 23,375 of 25,000 records; 1,250 were duplicates and 375 failed validation" lets readers judge. If excluded rows are concentrated in one region or customer type, say so, because it can bias the results.
Check sensitivity. For decisions that could change the conclusion, such as mean versus median imputation or capping at the 99th versus 95th percentile, run the analysis both ways. If the conclusion holds, say so; if it flips, that is itself a finding.
Respect privacy. Cleaning often touches personal data. Remove or mask fields the analysis doesn't need, such as names, emails and exact birth dates, and never copy raw personal data into scratch files and spreadsheets that escape your organization's controls.
Step 10 of 12Practice · Select all that apply
Which items belong in a cleaning log or report that accompanies an analysis? Select all that apply.
Select all that apply.
Step 11 of 12Practice · What would you do?
What would you do?
A regional manager reviews your cleaned dataset and asks you to drop three stores with unusually low sales "because they are outliers and make the region look bad". The stores are real, open and correctly recorded. What do you do?
Step 12 of 12Practice · True or false
A cleaning step is idempotent if applying it a second time to its own output changes nothing.
Lesson complete!