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.

#
010012026-03-01RiversideCroissantBakery13.53.5NewCard
110022026-03-01DowntownMuffinBakery4312ReturningCard
210032026-03-01RiversideBagelFood14.254.25NewCash
310042026-03-02AirportMuffinBakery133ReturningCash
410052026-03-02AirportChaiTea144ReturningApp
510062026-03-02DowntownCroissantBakery23.57ReturningCard
610072026-03-02CampusEspressoCoffee236NewCard
710082026-03-03RiversideLatteCoffee24.59ReturningCard
810092026-03-03AirportBagelFood24.258.5ReturningApp
910102026-03-04DowntownCroissantBakery43.514NewCard
1010112026-03-04DowntownMuffinBakery133ReturningCard
1110122026-03-05AirportGreen teaTea13.253.25ReturningApp
1210132026-03-06CampusEspressoCoffee133ReturningCash
1310142026-03-06CampusSandwichFood28.517NewCard
1410152026-03-07AirportGreen teaTea23.256.5ReturningCash
1510162026-03-07DowntownChaiTea144NewApp
1610172026-03-07AirportChaiTea144ReturningApp
1710182026-03-07CampusMuffinBakery236NewCard
1810192026-03-07AirportBagelFood14.254.25NewCash
1910202026-03-08AirportMuffinBakery4312NewCard
2010212026-03-08DowntownBagelFood14.254.25ReturningCash
2110222026-03-10RiversideSandwichFood18.58.5ReturningCard
2210232026-03-10CampusCroissantBakery23.57ReturningCard
2310242026-03-10AirportLatteCoffee14.54.5NewCash
2410252026-03-10DowntownSandwichFood48.534NewCard
2510262026-03-11DowntownLatteCoffee44.518ReturningCard
2610272026-03-11AirportCold brewCoffee14.754.75NewApp
2710282026-03-11CampusEspressoCoffee133NewCard
2810292026-03-11RiversideEspressoCoffee133NewCard
2910302026-03-13CampusBagelFood14.254.25NewCard

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

ProblemFind itFix it
Missing valuesdf.isna().sum()dropna(subset=[…]), fillna(value), or rebuild from other columns
Duplicate rowsdf.duplicated().sum()drop_duplicates() (or on a key: subset=["order_id"])
Inconsistent textdf["branch"].unique().str.strip().str.title(), .replace({…})
Wrong typesdf.dtypesastype(int), pd.to_numeric(…, errors="coerce"), pd.to_datetime
Impossible valuesdescribe(), a business ruleFix 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_missing so 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

Question 1

1.Which counts missing values per column?

Question 2

2.'Airport', 'airport' and 'Airport ' should become one value. Use…

Question 3

3.qty is missing but total and unit_price are known. Best fix?

Question 4

4.pd.to_numeric(s, errors='coerce') does what with 'n/a'?

Quiz score: 0 of 4

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

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.