Find missing values in a pandas DataFrame
Build a missing-value mask, count gaps by column, and select incomplete rows without confusing missing data with empty strings or infinity.
Use df.isna() to create a Boolean DataFrame with the same shape as df. A True cell marks a missing value. isnull() is an alias; choose one spelling consistently.
Create a missing-value mask
import numpy as np
import pandas as pd
df = pd.DataFrame({
"A": [1, np.nan, 3],
"B": [np.nan, 5, np.nan],
"C": [7, 8, 9],
})
mask = df.isna()
print(mask)
# A B C
# 0 False True False
# 1 True False False
# 2 False True False
The mask answers where values are missing. Reductions answer how much data is affected:
columns_with_gaps = mask.any() # A=True, B=True, C=False
missing_per_column = mask.sum() # A=1, B=2, C=0
missing_total = mask.sum().sum() # 3
missing_fraction = mask.mean().round(3) # A=0.333, B=0.667, C=0.0
has_any_missing = mask.any().any() # True
For a zero-row DataFrame, a per-column missing fraction has no observations to average; do not interpret an undefined rate as evidence of complete data.
Select the rows or columns that matter
incomplete_rows = df[df.isna().any(axis=1)]
missing_b = df[df["B"].isna()]
complete_rows = df.dropna()
selected_counts = df[["A", "B"]].isna().sum()
a_missing_b_present = df[df["A"].isna() & df["B"].notna()]
axis=1 reduces across columns for each row. dropna() returns a new DataFrame by default; decide whether dropping incomplete rows is appropriate before discarding them.
Distinguish missing values from other unusual values
Depending on dtype, missing data may be represented by np.nan, None, pd.NaT, or pd.NA. isna() handles those representations. Empty strings, the literal string "nan", and positive or negative infinity are not missing by default.
If the data contract treats particular values as missing, normalize them explicitly:
normalized = df.replace("", np.nan)
normalized = normalized.replace([np.inf, -np.inf], np.nan)
Do not apply that policy automatically to every dataset: an empty string or infinity can have a meaningful domain interpretation.
FAQ
How do I check one value?
pd.isna(value) returns a Boolean for a scalar and a corresponding mask for array-like input. Check the shape of the result before using it in an if statement.
Why does comparison with np.nan fail?
Floating-point NaN does not compare equal to itself. Nullable pandas values can also propagate an unknown result through comparisons. Use the missing-value predicate rather than == np.nan or a self-inequality trick.
Can I inspect only a subset of columns?
Yes. Select those columns first, then apply isna() and the reduction you need. This keeps unrelated optional fields from determining whether a row is complete.
References: pandas missing-value masks, Working with missing data.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan