Skip to the workspace
Pebellslearn by building
Cleaning Data  ›  Lesson

Finding Missing Values

isnull, isna 12 minisna marks every missing cell as True, so summing it counts gaps per column, and NaN must be detected this way because it never equals itself.

python data analysis / Finding Missing Values

Explore each scene at your own pace.

01 · The rule

The rule

Finding Missing Values

Last night's export arrived with holes: the till crashed twice and one clerk skipped a field. Any average you compute now is quietly averaging over gaps. Before fixing missing values, you have to find them all and know where they sit.

The rule

Ask isna, then sum: missing values counted per column.

Here is the raw export, loaded as raw:

date branch genre copies price
0 2024-03-01 Pune fiction 3.0 250.0
1 2024-03-01 Delhi science 5.0 400.0
2 2024-03-02 Pune science NaN 400.0
3 2024-03-02 Chennai NaN 4.0 250.0
4 2024-03-03 Delhi fiction 1.0 NaN
5 2024-03-03 Chennai comics 6.0 120.0
6 2024-03-04 Pune comics NaN 120.0
7 2024-03-04 Delhi science 3.0 400.0

pandas marks a missing value as NaN ("not a number"); a missing None in a text column also counts as missing. Notice copies shows 3.0 rather than 3: a column of whole numbers with a NaN in it is stored as floats.

raw.isna()              # same-shape table of True/False
raw.isna().sum()        # date 0, branch 0, genre 1, copies 2, price 1
raw.isna().sum().sum()  # 4 missing cells in total
raw.isna().any(axis=1)  # True for each row with at least one gap
raw[raw.isna().any(axis=1)]   # show those rows: 2, 3, 4 and 6

isnull() is exactly the same method under an older name; notna() is the opposite. raw.info() also shows the non-missing count per column, a quick way to spot gaps.

Why a special method? Because NaN is not equal to anything, not even itself:

import numpy as np
np.nan == np.nan    # False

So raw["copies"] == np.nan is False on every row and finds nothing. Always use isna().

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.