Unit 5 · Data · Lesson 5.6

Dirty Data

Real data is messy: blanks, “N/A,” a 999 that means “didn’t answer,” the same city spelled three ways. Cleaning it isn’t janitorial work — it’s a series of judgment calls, and every one changes the answer.

Big Idea 2 — DataCED 2.3 · Week 2 begins

Before this lesson

No reading — this page is the reading. Runestone’s datasets come pre-cleaned, which is exactly why we need this lesson.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Big Data. Week 1 got data in and asked it questions. Week 2 is about trust: what you had to do to the data first, and how far you can push the answer.

2

The Hook

What crashed your code in Lab 5.5? Somewhere in that file was a value your program couldn’t use — or worse, one it used without complaining and got a ridiculous answer. That wasn’t your bug. That was the data.

Today’s practice set has every classic problem in 18 rows: three kinds of blank, a code pretending to be a number, and one city spelled four ways.

3

The Idea

Data cleaning is finding and handling values that would make a result wrong. The usual suspects:

ProblemLooks likeTypical rules
Missing datablank, N/A, a spacedrop the row · fill with a value · leave it and skip it per question
Placeholder codes999, -1, 0treat as missing, not as a number
Inconsistent textBrea, brea, BREA, Brea normalize: trim spaces, one capitalization
Outliersa value far from the restcheck it: impossible → error; unusual but real → keep
Duplicatesthe same record twiceremove the extra copy
# Rule: blank, "N/A", or spaces → missing. 999 → missing. Missing rows are skipped.
hours = []
for row in rows:
    h = row["hours"].strip()                 # "  " becomes ""
    if h == "" or h == "N/A" or h == "999":
        continue                             # skip this row
    hours.append(int(h))

city = row["city"].strip().title()          # "brea " → "Brea"
Every cleaning decision is a choice that changes the answer. So write down what you decided and why. And watch for the dangerous case: dropping incomplete rows is not neutral if the incomplete ones have something in common.
4

Try It — The Dirty-Data Sandbox

18 rows from a summer-jobs survey: id, city, hours_per_week. Flip each rule and watch the answers move. Same data, different decisions, different results.

missing hours (blank, N/A, space):
999:
city spelling:
Show the raw rows

In class

Find every problem in the sandbox’s raw rows. For each, write a rule and one sentence of why. Then compare with a neighbor: different rules? Different answers? Both defensible?

5

Vocabulary

data cleaning Big Data
Finding and fixing or removing incorrect, incomplete, or inconsistent data so it can be processed correctly.
missing data Big Data
Values that were never recorded — blanks, N/A, or placeholder codes like 999.
outlier Big Data
A value far from the others. It may be an error (an age of 999) or real but unusual — check before removing.
6

Check

Five questions. The recurring shape: which of these could distort the result?

0 of 5 answered
7

Impact Check

Who gets cleaned out?

Missing data is rarely random. People skip questions they find uncomfortable; clinics with fewer staff leave more fields blank; forms that only accept certain name formats reject the rest. Drop the incomplete rows and you may have quietly dropped a whole group — then published a result about “everyone.”

8

Connections

Came from Lab 5.5 — the value that wrecked your average now has a name and a rule.
Next in 5.7 — with clean data, filter to a subset and count by category.
Returns in Lab 5.8 — Part 1 is four cleaning rules on the club dataset — written down, applied consistently.
Returns in Mini-Project 4 — “started with ___ rows, used ___” — and what the dropped rows had in common.
← 5.5 Lab: First Data Dive