Cleaning Messy Data
Fix duplicates, inconsistent formats and typos, choose sensible strategies for missing values, and investigate outliers instead of deleting them blindly.
Welcome back!
Step 1 of 11Learn
Four ways to spell New York
An analyst summarizes sales by city and finds four separate rows near the top: "New York", "new york", "NY" and "New York " (with a trailing space). Each looks like a mid-sized market. Combined, New York is actually the company's largest city by far.
This is what real data looks like. It's collected by different systems, typed by different people, exported in different formats and merged from different sources. Surveys of data professionals regularly find that preparing and cleaning data takes a large share of their working time, often more than the analysis itself.
Data cleaning means finding and fixing errors, gaps and inconsistencies so the data accurately represent reality. Common problems:
- Duplicates: the same record entered twice, often from merged systems or repeated form submissions.
- Inconsistent categories: "NY" vs. "New York", "M" vs. "Male", "Credit card" vs. "credit_card".
- Extra whitespace and capitalization differences that make identical values look different to software.
- Wrong data types: numbers stored as text ("1,200" or "$45"), so they can't be summed; dates stored as text in mixed formats.
- Inconsistent units and formats: prices in dollars and cents, weights in pounds and kilograms, dates as 03/04/2026 meaning March 4 in one file and April 3 in another.
- Impossible values: an age of 212, a negative quantity, an order date in the future.
- Missing values: blank cells, or placeholders like "N/A", "-", 0 or 999 that mean "unknown".
None of these is hard to fix individually. The skill is in systematically looking for them before trusting any summary.
Step 2 of 11Practice · Chart
Sales by city, before cleaning
Before cleaning, which city looks like the biggest market?
Tap the bar that answers the question, then press Check.
Step 3 of 11Practice · Spot the mistake
Here are rows from a customer table with columns name | state | signup date | age. Tap the three rows with data quality problems.
Step 4 of 11Learn
A cleaning checklist
Clean data systematically, not by scrolling and hoping to notice problems. A practical sequence:
- Keep the raw data untouched. Work on a copy, and record every cleaning step. If something goes wrong, or someone asks how a number was produced, you can retrace it.
- Check the shape. How many rows and columns? Does the row count match what you expected, such as the number of orders in the source system?
- Check types. Are numbers stored as numbers and dates as dates? Remove currency symbols and thousands separators before converting.
- Standardize text. Trim spaces, make capitalization consistent and map variants to one value ("NY", "New York", "new york" → "New York").
- Remove duplicates, but define what counts as a duplicate. Two orders from the same customer on the same day might be legitimate. Use a unique ID when one exists.
- Validate ranges and rules. Ages between 0 and 120, quantities above zero, end dates after start dates, percentages between 0 and 100.
- Handle missing values deliberately (next step).
- Investigate outliers.
- Re-check totals against a trusted source, such as the finance report's monthly revenue.
A quick, powerful habit is to look at the unique values and counts of every categorical column. A list showing "Male: 4,210 | Female: 4,388 | M: 37 | F: 41 | (blank): 112" tells you instantly what needs fixing. For numerical columns, check the minimum, maximum and count: impossible extremes jump out.
Step 5 of 11Diagram
A cleaning sequence
Tap a shape with to see more.
- Copy the raw data
Never edit the original; log every step.
- Check shape
- Check types
- Standardize text
- Remove duplicates
Define what counts as a duplicate first.
- Validate ranges
- Missing values, outliers
- Re-check totals
Against a trusted source like the finance report.
Step 6 of 11Learn
What to do with missing values
Missing data is almost unavoidable. What matters is why it's missing and how you handle it.
First, find it. Missing values hide behind blanks and also placeholders like "N/A", "unknown", "-", and suspicious defaults such as 0 or 999. A column of incomes containing many zeros might mean "not provided", and treating those zeros as real would drag the average far down.
Then ask whether the missingness is random. If a sensor occasionally fails at random, the missing readings probably don't distort the overall picture. But if higher-income people are more likely to skip an income question, the remaining answers understate typical income. Missing data that is related to the value itself is the dangerous kind.
Common strategies:
- Leave it out of a specific calculation (for example, compute average income only among those who answered), and report how many were excluded.
- Drop rows with missing values, which is fine when few rows are affected and the missingness looks random, but it can bias results if many are missing or the missing cases differ.
- Fill in (impute) values, such as using the median of the column or a value from a related record. Useful but risky: it can make data look more certain than it is. Always flag imputed values.
- Treat "missing" as its own category for categorical variables. "Did not answer" can itself be informative.
- Go back to the source to recover the true values, often the best option when it's possible.
Whatever you choose, document it and report it. "Average order value: $54 (based on 9,412 of 10,000 orders; 588 had missing amounts)" is honest and useful.
Step 7 of 11Practice · What would you do?
What would you do?
In an employee survey, 30% of people left the salary question blank. Most blanks are from senior staff. Your manager wants the average salary of respondents. What should you do?
Step 8 of 11Learn
Outliers: investigate, don't just delete
An outlier is a value far from most of the others: a $48,000 order in a store where typical orders are around $50, or a delivery time of 61 days when most take 2 to 5.
Outliers have three possible explanations, and each calls for a different response:
- Error: a typo, a units mix-up (cents recorded as dollars), a test transaction. Fix it if you can find the true value; otherwise exclude it and note why.
- Legitimate but unusual: a real bulk order from a corporate client. Keep it, but consider reporting results with and without it, or using the median, which isn't pulled around by extremes.
- The most interesting finding: a sudden spike in returns, a fraud pattern, a server failure. Sometimes the outlier is the story.
A common rule of thumb flags values more than 1.5 × IQR beyond the quartiles. The interquartile range (IQR) is the spread of the middle half of the data: the 75th percentile (Q3) minus the 25th percentile (Q1). If Q1 = 20 and Q3 = 40, the IQR is 20, so values below 20 − 30 = −10 or above 40 + 30 = 70 are flagged as potential outliers. Another quick check is to look for values more than about 3 standard deviations from the mean.
These rules flag values for investigation, not automatic deletion. Deleting every inconvenient point is one of the easiest ways to make an analysis say what you want it to say, and it can hide exactly the problem you were hired to find.
Step 9 of 11Diagram
Delivery times and the 1.5 × IQR fence
Tap a shape with to see more.
- Q1 = 2
2 days
- Q3 = 5
5 days
- Upper fence = 9.5
Q3 + 1.5 × (5 − 2) = 9.5 days.
- 61-day delivery
Far past the fence: investigate before deciding.
Step 10 of 11Practice · Work it out
Order values have Q1 = $30 and Q3 = $70. Using the 1.5 × IQR rule, above what order value would a point be flagged as a potential high outlier, in dollars?
Step 11 of 11Practice · Choose one
A dataset of product weights shows one item at 2,300 kg, while all others weigh between 1 and 5 kg. What is the best first step?
Lesson complete!