Lesson 2 of 5 · 30 min

Build Data Cleaning and Validation Pipelines

Clean data in code, repeatably, with checks that fail loudly.

XDQ Tutor · AI guide

Hello. In this lesson I will take you through 4 ideas, one at a time.

  1. Clean in code, not by hand
  2. Validation checks
  3. Missing values
  4. Lineage

Why it matters: Clean data in code, repeatably, with checks that fail loudly. After each idea I will ask you one quick question.

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.

Quick check

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.

python · illustrative
def validate(df):
    errors = []
    if df["order_id"].duplicated().any(): errors.append("duplicate order_id")
    if df["amount"].lt(0).any(): errors.append("negative amount")
    if df["country"].isna().mean() > 0.02: errors.append("country missing > 2%")
    if not 0.8 < len(df) / expected_rows < 1.2: errors.append("row count outside range")
    if errors: raise ValueError(errors)
Quick check

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.

Quick check

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.

Key takeaways
  • 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
Try it

Write a cleaning script and five validation checks for one source from your audit. Make the pipeline stop and alert when a check fails.

That is the lesson.
Mark it complete to track your progress on this device.
← PreviousAudit Your Data Before Starting an AI ProjectNext lesson →Labelling Guidelines and Measuring Agreement