Loading
0x20Lesson 3 of 10

Inspect and clean a dataset

Find missing values, duplicates, inconsistent labels, wrong units and outliers - and handle each deliberately.

24 min 6-question quiz 3 code exercises
By the end of this lesson you can
  • 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, bicycle vs bike.
  • 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 XP

Data quality hunt

Here’s a raw export. Click every value that needs cleaning before analysis.

trips.csv (round 1 of 1)Flags found 0/0

Click every suspicious section. There are 7.

trip_id,start,minutes,mode,member 101,2026-03-02 08:14,12,bus,yes 102,2026-03-02 08:20,-1,walk,no 102,2026-03-02 08:20,-1,walk,no 103,03/02/2026 09:05,9, Bus,yes 104,2026-03-02 09:40,1260,bike,Y 105,2026-03-02 10:02,15,bicycle,no

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 type

Outliers: 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.

quartiles.py
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)
Output
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.

Exercise 1

Average known values

+25 XP

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
main.py
Loading editor…

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.

Exercise 2

Clean duplicates and categories

+25 XP

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
main.py
Loading editor…

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.

Exercise 3

Flag outliers with the IQR rule

+25 XP

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
main.py
Loading editor…

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…