Cleaning Messy Data
Lesson 2 of 5 · View course roadmap
Learn the idea
Real datasets arrive filthy: stray spaces, empty cells, "n/a", numbers stored as text. Analysts joke that the job is 80% cleaning — and the pattern is always the same funnel:
value.strip()— cut the whitespace first- Skip empties early:
if not value: continue - Convert inside a safety net:
try: int(value) except ValueError: skip
The golden rule: never let one bad value kill the whole run. Quarantine what you can't parse, keep what you can, and (in real jobs) count what you skipped — a spike in bad rows is itself a finding.
Where you'll use this
Analysts genuinely spend most of their time here: exported CSVs with stray spaces, 'N/A', euro signs and thousands separators. Robust cleaning is why senior analysts' numbers are trusted.
Common mistakes
- Cleaning while iterating the same list you're modifying — build a new clean list instead.
- int("3.5") raises ValueError — parse decimals with float() first if decimals are possible.
- Silently dropping bad rows without counting them — in real work, the skip count is itself a data-quality metric.
Pro tip
Normalise aggressively before converting: value.strip().lower().replace(",", "").removeprefix("€") handles most European CSV horrors in one chain.
Try it yourself
The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.
Output appears here…
Pass the challenge +35 XP
Clean raw = [" 42 ", "17", "oops", "", "23 ", "n/a", "8"] into a list of ints, silently skipping anything that isn't a number. Print the clean list, then its sum.
[42, 17, 23, 8] 90
Loop, strip each value, try int(value) and append; except ValueError: pass. Empty strings raise ValueError too, so the try handles them.
raw = [" 42 ", "17", "oops", "", "23 ", "n/a", "8"]
clean = []
for value in raw:
try:
clean.append(int(value.strip()))
except ValueError:
pass
print(clean)
print(sum(clean))
Run your code to check it…
Check your understanding
Your notes (saved on this device)
Tip: use ← and → to move between lessons, ⌘K to search everything.