Inspect and clean a dataset
Find missing values, duplicates, inconsistent labels, wrong units and outliers - and handle each deliberately.
- Spot common data quality problems
- Handle missing values and duplicates deliberately
- Standardize categories and flag outliers with the IQR rule
Real data is messy, and cleaning often takes most of a project. Look for:
- Missing values - blank,
NA,-1,999. Missing isn’t zero: it may mean unknown, not recorded or not applicable. - Duplicates - the same observation recorded twice.
- Inconsistent categories -
Bus,bus,BUS,bicyclevsbike. - Units and types - minutes mixed with seconds, numbers stored as text, dates in two formats.
- Impossible or extreme values - a 9,000-minute trip, a negative age.
For each problem, choose a rule, apply it in code (never by hand in a spreadsheet), and write it down - your conclusions depend on these choices.
Try it
Up to 15 XPData quality hunt
Here’s a raw export. Click every value that needs cleaning before analysis.
Click every suspicious section. There are 7.
In pandas, the common fixes are one line each:
1trips["minutes"] = trips["minutes"].replace(-1, pd.NA) # sentinel → missing
2trips = trips.drop_duplicates(subset="trip_id") # keep the first copy
3trips["mode"] = trips["mode"].str.strip().str.lower() # " Bus" → "bus"
4trips["mode"] = trips["mode"].replace({"bicycle": "bike"}) # merge aliases
5trips["start"] = pd.to_datetime(trips["start"], format="mixed") # one date typeOutliers: flag, then think
John Tukey’s rule flags values more than 1.5 × IQR below the first quartile (Q1) or above the third (Q3), where the interquartile range IQR = Q3 − Q1 is the spread of the middle half of the data. A flagged value is a question, not a verdict: it may be an error to fix, or the most interesting observation in the dataset. Never delete outliers just to make results look tidier.
1import statistics
2minutes = [12, 15, 18, 20, 21, 22, 25, 27, 30, 75]
3q1, q2, q3 = statistics.quantiles(minutes, n=4)
4print(q1, q2, q3)17.25 21.5 27.75
Key takeaways
Check missing values, duplicates, categories, units and extreme values before analysis.
Clean in code with explicit, documented rules.
Flag outliers with the IQR rule, then investigate instead of deleting.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Average known values
Read a JSON array of numbers and null values. Print the average of the known numbers. The input has at least one known number.
- One missing score
- No missing values
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Clean duplicates and categories
Read a CSV with trip_id and mode. Drop duplicate trips (keep the first), standardize mode by stripping spaces and lowercasing, and map bicycle to bike. Print duplicates removed: N, then each mode and its count, most common first (ties alphabetically).
- Messy modes
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Flag outliers with the IQR rule
Read a JSON array of numbers. Using statistics.quantiles(values, n=4), print Q1=… Q3=… IQR=…, then fences: LOW to HIGH (Q1 − 1.5×IQR and Q3 + 1.5×IQR), then outliers: [...] (in input order). Format every number with :g.
- Commute times
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…