Cleaning data and missing values
Real-world tables rarely arrive ready for a model. Inspecting formats, errors, and gaps—and knowing that every cleaning choice carries an assumption—helps you judge whether a result can be trusted.
Inspecting messy data before a model sees it.
Think of checking ingredients before cooking: you read mismatched jar labels, set aside anything spoiled, and notice what is missing from the pantry. Data cleaning is the step where you inspect and repair a dataset before summarising it or training a model.
The kitchen analogy only goes so far: in data work, deciding what to drop, replace, or keep visible can change the numbers and conclusions you get at the end.
This lesson covers four ideas in order:
- Format mismatches: the same real value written in inconsistent ways.
- Data errors: recorded values that are wrong or impossible.
- Missing values (gaps): cells where no value was recorded.
- Cleaning choices: deciding whether to fix, drop, fill, or flag problems—and how each choice changes the result.
Nadia (an invented character for this lesson) helps run a volunteer meal-delivery co-op serving two areas—Town Centre and Hilltop. She checks six recent deliveries (an invented table) to see whether the co-op’s overall average delivery time stays under a 30-minute target, and whether Hilltop looks slower than Town Centre.
| Row | Area | Time (mins) | Problem spotted |
|---|---|---|---|
| D1 | Town Centre | 18 | Clean |
| D2 | town centre | 22 | Format mismatch |
| D3 | Town Centre | 20 mins | Format mismatch |
| D4 | Hilltop | -15 | Data error |
| D5 | Hilltop | (blank) | Missing value |
| D6 | Hilltop | 44 | Clean |
If Nadia runs a plain numeric average on this raw table without cleaning it first, software skips 20 mins (text) and (blank), averages 18, 22, -15, 44, and reports an overall average of 17.3 min—artificially low because of -15.

Spotting format mismatches and errors.
In this lesson, we sort cell defects by one simple test: what went wrong in that cell. Real tables can mix more than one problem in the same row, so inspecting columns carefully matters.
Format mismatch
The real value is valid, but written inconsistently so software treats matching records differently.
In Nadia’s log: town centre in D2 creates a separate area label from Town Centre, and 20 mins in D3 mixes text into a number column.
Everyday example: writing a date as 03/04/2026 means 3 April in DD/MM/YYYY format but 4 March in MM/DD/YYYY format.
Data error
A recorded value is wrong or impossible—often from a typo, a broken sensor, or a unit mix-up.
In Nadia’s log: -15 in D4 is an impossible negative delivery time. Without a paper receipt to verify the true time, Nadia marks D4 as missing instead of guessing.
Everyday example: a fitness watch slipping off a live wearer’s wrist and logging a heart rate of 0 beats per minute—an impossible live reading (even though 0 can be a genuine measurement in other datasets).
Impossible errors like -15 minutes stand out quickly. Plausible typos—such as typing 24 instead of 42—are much harder to spot unless you check the original source, and some older systems store placeholder codes like -999 to stand in for missing data.
Missing values—and why cleaning is not neutral.
A missing value (or gap) is a cell where no measurement was recorded—shown as a blank cell, N/A, or null. Gaps can happen at random, or they can follow a pattern tied to a specific group or situation.
Once Nadia fixes the format mismatches (D2 and D3) and marks the impossible -15 (D4) as missing, Town Centre has three valid times (18, 22, 20—average 20 min) while Hilltop has one valid time (44) and two missing times (D4 and D5). Look at how different missing-value choices change her numbers:
- Drop incomplete rows: overall average is 26 min (under the 30-min target), but Hilltop drops from half the log (3 of 6) to 1 of 4 rows—under-counting Hilltop.
- Fill with the overall average (26 min): keeps 6 rows and an overall average of 26 min, but borrows faster Town Centre times and pulls Hilltop’s average down to 32 min.
- Fill with Hilltop’s known average (44 min): pushes the overall average to 32 min (over the 30-min target), but assumes a single recorded Hilltop trip represents all Hilltop deliveries.
- Fill with 0 (a common spreadsheet pitfall): treats missing deliveries as 0-minute trips, collapsing the overall average to 17.3 min and making Hilltop (14.7 min) look faster than Town Centre.
- Keep visible and flag missing: adds a
Missing flagcolumn, averages valid times only (26 min across 4 rows), and makes it obvious that both gaps are in Hilltop—pointing Nadia to investigate weak mobile signal on the hill.
Even after cleaning, one valid Hilltop delivery (44 min) is far too little evidence to decide staffing on its own—Nadia needs more complete Hilltop records first.
Suppose a clinic sends an online follow-up survey, and older patients are more likely to leave an app-usability question blank. Dropping every row with a blank answer does not stay neutral—it silently removes older patients’ feedback from the results.

Fix Nadia’s messy table yourself.
Start with the raw log: -15 pulls the raw average down to 17.3 min and town centre splits Town Centre into two labels. Fixing D2 and D3 standardises formats, and marking D4 (-15) as missing clears the impossible value—then switching missing-value strategies shows how the overall average moves from 17.3 min to 26 min to 32 min.
- Notice the starting raw summary: 3 area labels and a distorted 17.3 min overall average.
- Click Fix all formats & errors (or toggle D2, D3, and D4 one by one) so formats match and
-15is marked missing. - Compare Drop incomplete rows, Fill with Hilltop average, and Fill with 0 (pitfall)—watch both the overall average and Hilltop’s share of rows change.
- Click Keep visible & flag missing, then click Reset table to start over.
| Row | Area | Time | Status |
|---|---|---|---|
| D1 | Town Centre | 18 | Clean |
| D2 | town centre | 22 | Format mismatch |
| D3 | Town Centre | 20 mins | Format mismatch |
| D4 | Hilltop | -15 | Data error |
| D5 | Hilltop | (blank) | Yes (flagged) |
| D6 | Hilltop | 44 | Clean |
Unfixed issues remain (3 left): D2 ("town centre") splits Town Centre into two labels; D3 ("20 mins") is skipped by numeric math; D4 (-15) pulls the overall average artificially low. Fix D2, D3, and D4 first, then compare the missing-value strategies.
Illustrative simulation: these six deliveries and simplified cleaning rules are invented for this lesson to show how cleaning choices shift summaries. A real co-op would not decide staffing from six deliveries or one valid Hilltop trip.
Good habits and limits of data cleaning.
Because cleaning changes what a dataset says, reliable teams treat cleaning choices as part of the evidence—not invisible housekeeping.
- Keep an untouched raw copy: never overwrite the original file so any cleaning step can be audited or reversed.
- Record every rule: write down what you standardised, dropped, or filled—and why—so others can judge the result.
- Ask why values are missing: checking who or what is missing connects directly to collecting representative data (Lesson 5.1).
- Avoid peeking at test data: when filling gaps for a machine learning model, calculate fill values using the training split only so test data does not leak into training (Lesson 5.9).
- Is dropping rows with missing values the safest default?
Not necessarily. When one group has more missing values, dropping incomplete rows shrinks that group’s representation and can bias the results.
- Should I replace blank numbers with 0 so formulas do not break?
Usually no. Zero is a real measurement, so replacing a blank cell with 0 can pull averages and model predictions in the wrong direction.
- Does clean data guarantee an accurate model?
No. Cleaning repairs format mismatches, errors, and gaps in the records you have, but it cannot fix a sample that left out whole groups (Lesson 5.1) or guarantee future predictions are right.
Check your understanding.
Take this with you.
Data cleaning is not neutral. Before training or summarising, inspect your table for format mismatches, data errors, and missing values. Every choice to drop, fill, or flag a gap carries an assumption that can shift averages, shrink a group’s representation, and change the decision you make—so inspect why values are missing and record every rule you apply.
Ready to continue? Return to Data and Evaluation.