Sub topic 11
Spotting missing data, wrong dtypes, and outliers
This chapter assumes you can run Python, have pandas imported as pd, and have already seen head, info, and describe on their own. Here you use their output together to find three common problems before you clean or analyse data.
Which method shows which problem
Each problem leaves a different trace.
| Problem | Where it shows up | What to look for |
|---|---|---|
| Missing data | `info`, `isna().sum()`, `describe` | Non-null count below the row count, or `count` below the row count |
| Wrong dtype | `info`, `dtypes`, `describe` | `object` for a column that should be numeric, or a column missing from `describe` |
| Outliers | `describe` | `max` or `min` far from the quartiles, or a mean far from the median |
A deliberately messy example
The frame below has three planted problems. One age is missing. The salary values were read as text. One score has an extra digit.
import numpy as np
import pandas as pd
raw = pd.DataFrame({
"name": ["Ana", "Ben", "Cleo", "Dev", "Eli"],
"age": [34, 28, np.nan, 45, 39],
"salary": ["52000", "48000", "61000", "75000", "580000"],
"score": [71, 68, 74, 70, 990],
})
raw.info()
print(raw.isna().sum())
print(raw.describe())Reading the evidence
Missing data
In the info output, age reports 4 non-null values out of 5 entries. The index range line gives the row count, so any column with a smaller non-null count has gaps. raw.isna().sum() confirms this by printing 1 for age and 0 for the others.
age is also float64 even though every age is a whole number. A missing value is stored as NaN, which is a float, so one gap converts the whole column to floats.
Wrong dtypes
salary shows object. Pandas uses object for text, so the digits were stored as strings. Two signs point to this. info lists the dtype, and describe() leaves salary out because by default it summarizes only numeric columns.
Convert it, then check again:
raw["salary"] = pd.to_numeric(raw["salary"])
print(raw.dtypes)
print(raw.describe())salary is now int64 and appears in the summary. If a column holds values that cannot be parsed, pd.to_numeric raises an error. That error is useful, because it shows you the bad value. Passing errors="coerce" turns unparseable values into NaN instead. Use it only after you have looked at what would be lost.
Outliers
In the describe output for score, compare these rows:
meanis 254.6 and50%(the median) is 71.0.maxis 990 while75%is 74.0.
The mean is pulled upward by a single large value, while the median barely moves. A gap this large between mean and median, or between max and the 75% quartile, marks a value worth checking. It could be a typo, a unit error, or a real extreme.
describe does not tell you which row is responsible. Find it with a filter:
print(raw[raw["score"] > 200])Decide what to do only after you know the cause. Do not delete a value just because it is large.
Common errors
- Trusting
describefor completeness. Its output lists only numeric columns by default, so a column that is missing from it may have the wrong dtype. Usedescribe(include="all")to see every column. - Reading
meanalone. The mean hides outliers. Always compare it with the median. - Treating
float64as a mistake. An integer column with gaps is expected to be float. - Assuming that no
NaNmeans clean data. Placeholders such as"?","n/a", or-999are ordinary values to pandas. They show up asobjectdtype or as extreme minimums.
Exercise
A DataFrame has 200 rows. In info, the column price shows 188 non-null values and dtype object. In describe(), price does not appear. What two problems does this suggest, and what is one way to investigate the dtype?
Show answer
There are 12 missing values (200 - 188), and the column holds text instead of numbers, which is why describe() skips it. To investigate, call pd.to_numeric(df["price"], errors="coerce") and compare the resulting NaN count with the original 12, or run df["price"].head() and df["price"].unique() to look for placeholder strings such as "n/a" or currency symbols.