Data Cleaning Exercises

Module 19 · Analysis readiness · 7 min

Distribution shift

You'll be able to: Compare new data with the data a model or report was built on, and flag meaningful changes.

Before this: Fit on training data only

The idea

A cleaning rule or model tuned on last year's data can fail when this year's data looks different: new categories, larger amounts, more missing values.

Compare simple profiles between periods: share of missing values, category frequencies, quantiles.

Report shifts with numbers and decide with the owner whether rules need updating.

Worked example

Input: channel share: email 60% → 35%, chat 10% → 40%

Visual
Inspector → compare two periods
SQL (DuckDB)
SELECT period, channel, COUNT(*) * 1.0 / SUM(COUNT(*)) OVER (PARTITION BY period) FROM t GROUP BY ALL
pandas
pd.crosstab(df["period"], df["channel"], normalize="index")

Result: Chat grew fourfold; check the mapping and the model inputs

Common mistakes

  • Assuming stable data.
  • Only checking averages.
  • Silently changing rules.

Check yourself

Name two simple shift checks.

Missing-value share and category frequencies (or quantiles) per period.

Practice it

Fill gaps with a median learned from training data only.

Next: Fill gaps with a median learned from training data only