Build Data Cleaning and Validation Pipelines
Clean data in code, repeatably, with checks that fail loudly.
Hello. In this lesson I will take you through 4 ideas, one at a time.
Why it matters: Clean data in code, repeatably, with checks that fail loudly. After each idea I will ask you one quick question.
In this lessonFollows you as you read
Clean in code, not by hand
The short version: Manual cleaning in spreadsheets is not repeatable and hides decisions.
Manual cleaning in spreadsheets is not repeatable and hides decisions. Write cleaning steps as code: standardise formats, normalise units, deduplicate, handle missing values and map codes. Version the code with the project.
Keep raw data unchanged and write cleaned data to a separate layer.
Why clean in code rather than by hand?
Validation checks
The short version: Add checks that run on every load: required fields present, values in allowed ranges, unique keys unique, row counts within expected bounds, and referential integrity between tables.
Add checks that run on every load: required fields present, values in allowed ranges, unique keys unique, row counts within expected bounds, and referential integrity between tables. When a check fails, stop the pipeline and alert.
Silent bad data is worse than a failed job.
What should happen when a validation check fails?
Missing values
The short version: Decide per field: drop the record, fill with a default, infer from other fields or keep as missing with a flag.
Decide per field: drop the record, fill with a default, infer from other fields or keep as missing with a flag. Document the choice; it affects results.
Never fill missing values in a way that hides how much was missing.
What does lineage record?
Lineage
The short version: Record where each dataset came from, which code produced it and when.
Record where each dataset came from, which code produced it and when. When a model behaves oddly, lineage lets you trace the cause.
Store lineage alongside the data.
- Clean in versioned code, keep raw data unchanged
- Validate every load and fail loudly
- Decide and document missing value handling
- Record lineage for every dataset
Write a cleaning script and five validation checks for one source from your audit. Make the pipeline stop and alert when a check fails.
Mark it complete to track your progress on this device.