Module 5 · Pandas: Your Data Workbench · Lesson 16 of 51
Cleaning messy data
Find and fix missing values, duplicates, inconsistent text, wrong types, impossible values and date problems, and keep a cleaning log.
In this lesson · about 45 minutes
- Count missing values and choose to drop, fill or flag them
- Remove duplicates and standardise text with .str methods
- Fix types and catch impossible values with a sanity check
Try it · data lab
Spot the problems first
Switch to the raw till export and look for problems before using the button. Then read on to fix them in pandas.
Click a column name to sort by it; click again to reverse.
| # | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1001 | 2026-03-01 | Riverside | Croissant | Bakery | 1 | 3.5 | 3.5 | New | Card |
| 1 | 1002 | 2026-03-01 | Downtown | Muffin | Bakery | 4 | 3 | 12 | Returning | Card |
| 2 | 1003 | 2026-03-01 | Riverside | Bagel | Food | 1 | 4.25 | 4.25 | New | Cash |
| 3 | 1004 | 2026-03-02 | Airport | Muffin | Bakery | 1 | 3 | 3 | Returning | Cash |
| 4 | 1005 | 2026-03-02 | Airport | Chai | Tea | 1 | 4 | 4 | Returning | App |
| 5 | 1006 | 2026-03-02 | Downtown | Croissant | Bakery | 2 | 3.5 | 7 | Returning | Card |
| 6 | 1007 | 2026-03-02 | Campus | Espresso | Coffee | 2 | 3 | 6 | New | Card |
| 7 | 1008 | 2026-03-03 | Riverside | Latte | Coffee | 2 | 4.5 | 9 | Returning | Card |
| 8 | 1009 | 2026-03-03 | Airport | Bagel | Food | 2 | 4.25 | 8.5 | Returning | App |
| 9 | 1010 | 2026-03-04 | Downtown | Croissant | Bakery | 4 | 3.5 | 14 | New | Card |
| 10 | 1011 | 2026-03-04 | Downtown | Muffin | Bakery | 1 | 3 | 3 | Returning | Card |
| 11 | 1012 | 2026-03-05 | Airport | Green tea | Tea | 1 | 3.25 | 3.25 | Returning | App |
| 12 | 1013 | 2026-03-06 | Campus | Espresso | Coffee | 1 | 3 | 3 | Returning | Cash |
| 13 | 1014 | 2026-03-06 | Campus | Sandwich | Food | 2 | 8.5 | 17 | New | Card |
| 14 | 1015 | 2026-03-07 | Airport | Green tea | Tea | 2 | 3.25 | 6.5 | Returning | Cash |
| 15 | 1016 | 2026-03-07 | Downtown | Chai | Tea | 1 | 4 | 4 | New | App |
| 16 | 1017 | 2026-03-07 | Airport | Chai | Tea | 1 | 4 | 4 | Returning | App |
| 17 | 1018 | 2026-03-07 | Campus | Muffin | Bakery | 2 | 3 | 6 | New | Card |
| 18 | 1019 | 2026-03-07 | Airport | Bagel | Food | 1 | 4.25 | 4.25 | New | Cash |
| 19 | 1020 | 2026-03-08 | Airport | Muffin | Bakery | 4 | 3 | 12 | New | Card |
| 20 | 1021 | 2026-03-08 | Downtown | Bagel | Food | 1 | 4.25 | 4.25 | Returning | Cash |
| 21 | 1022 | 2026-03-10 | Riverside | Sandwich | Food | 1 | 8.5 | 8.5 | Returning | Card |
| 22 | 1023 | 2026-03-10 | Campus | Croissant | Bakery | 2 | 3.5 | 7 | Returning | Card |
| 23 | 1024 | 2026-03-10 | Airport | Latte | Coffee | 1 | 4.5 | 4.5 | New | Cash |
| 24 | 1025 | 2026-03-10 | Downtown | Sandwich | Food | 4 | 8.5 | 34 | New | Card |
| 25 | 1026 | 2026-03-11 | Downtown | Latte | Coffee | 4 | 4.5 | 18 | Returning | Card |
| 26 | 1027 | 2026-03-11 | Airport | Cold brew | Coffee | 1 | 4.75 | 4.75 | New | App |
| 27 | 1028 | 2026-03-11 | Campus | Espresso | Coffee | 1 | 3 | 3 | New | Card |
| 28 | 1029 | 2026-03-11 | Riverside | Espresso | Coffee | 1 | 3 | 3 | New | Card |
| 29 | 1030 | 2026-03-13 | Campus | Bagel | Food | 1 | 4.25 | 4.25 | New | Card |
30 rows × 10 columns
Column total: decimal number (float)
Values
30
Missing
0
Unique
17
Mean
7.52
Ranges from 3 to 34. This is what pandas' describe() reports for a number column.
A cleaning checklist
| Problem | Find it | Fix it |
|---|---|---|
| Missing values | df.isna().sum() | dropna(subset=[…]), fillna(value), or rebuild from other columns |
| Duplicate rows | df.duplicated().sum() | drop_duplicates() (or on a key: subset=["order_id"]) |
| Inconsistent text | df["branch"].unique() | .str.strip().str.title(), .replace({…}) |
| Wrong types | df.dtypes | astype(int), pd.to_numeric(…, errors="coerce"), pd.to_datetime |
| Impossible values | describe(), a business rule | Fix from another source, set to NaN, or drop, and log it |
Missing values: drop, fill or flag?
- Drop rows when few are missing and they're missing at random.
- Fill when you can work the value out (qty = total ÷ unit_price) or a sensible default exists ("Unknown" for a category). Filling a number with the median is common but hides uncertainty.
- Flag: add
qty_was_missingso models and readers know.
Ask why values are missing. If the app doesn't record customer type for cash payments, "missing" means "cash", which is information, not noise.
Diagnose the raw export
Clean it, step by step, with a log
Exercise
Clean the branch column and count
raw is the messy export. Make clean: a copy with duplicates dropped and branch stripped and title-cased. Then set counts to clean["branch"].value_counts().sort_index().
Code that runs before yours
import pandas as pd
raw = pd.read_csv("kc_orders_messy.csv")Exercise
Numbers stored as text
A supplier sheet has prices as text, some unusable. Convert s to numbers with pd.to_numeric(…, errors="coerce") (bad values become NaN) and store it in prices. Set n_bad to how many became NaN, and avg to the mean of the good ones, rounded to 2 decimals.
Check your understanding0 of 4 correct
Quiz score: 0 of 4
Exercises solved: 0 of 2
Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.