pandas isna and notna
df.isna() returns True wherever a value is missing (NaN, None or NaT) and notna() is the opposite. Chain it to count or filter: df.isna().sum() counts missing values per column, and df[df["col"].isna()] keeps the rows where col is missing. isnull and notnull are older names for the same functions.
Syntax
df.isna() | df.notna() | Series.isna() | pd.isna(value)Examples
import pandas as pd
import numpy as np
df = pd.DataFrame({"city": ["Lima", None, "Oslo", "Rome"],
"sales": [120.0, 80.0, np.nan, np.nan]})
df.isna()Result:
city sales
0 False False
1 True False
2 False True
3 False Truedf.isna().sum()Result:
city 1
sales 2
dtype: int64True counts as 1, so the sum is the number of missing values in each column.
df[df["sales"].isna()]Result:
city sales
2 Oslo NaN
3 Rome NaNRows where sales is missing.
df[df["sales"].notna()]Result:
city sales
0 Lima 120.0
1 None 80.0Rows that have a sales figure, whatever the other columns hold.
df.isna().mean().round(2)Result:
city 0.25
sales 0.50
dtype: float64The mean of True/False is the share missing: half of sales.
Try it yourself
Some players have no points recorded. Replace the missing points with 0, leave the missing assists as they are, and return the whole scores table.
scores: 6 rows, first 4 shown
| player | points | assists |
|---|---|---|
| "Ana" | 12 | 3 |
| "Bo" | NaN | 5 |
| "Cy" | 7 | NaN |
| "Di" | NaN | 2 |
Free, no account needed. Real pandas runs in your browser; the first run takes a few seconds to load Python.
Common mistakes
- Comparing with == np.nan: NaN is not equal to anything, including itself, so df["col"] == np.nan is False everywhere. Use isna().
- Empty strings and placeholders such as "n/a" or -1 are not missing to pandas. Replace them with NaN first, or read the file with na_values=["n/a"].
- Using is None in a filter: it tests the whole Series, not each value. Use isna() for element-wise checks.
More practice: isna and notna
These drills check your answer on live data, free, no account needed.
- Drop rows missing a key fieldDrop the contacts that have no email. Contacts without a phone number stay. Keep the original row labels.
Questions
What is the difference between isna and isnull?
None. isnull is an alias of isna, and notnull of notna. isna is the name used in current documentation.
How do I check if any value is missing?
df.isna().any() answers per column, df.isna().any().any() gives one True or False for the whole DataFrame.
How do I count rows with at least one missing value?
df.isna().any(axis=1).sum().
Related
- fillnaHow to fill NaN values in pandas with fillna: a constant, a different value per column, the column mean or median, forward and backward fill, and by group.
- dropnaHow to drop NaN rows in pandas with dropna: any or all missing, only certain columns with subset, drop columns with axis=1, thresh for a minimum of values.
- where, mask and np.whereHow to create a conditional column in pandas: np.where for if-else, np.select for several conditions, Series.where to keep values that pass a test, and mask.
- describeWhat df.describe returns in pandas: count, mean, std, min, quartiles and max for numeric columns, including text columns, custom percentiles and transposing.
Interview and translation guides
- Timed mock interview
- pandas interview questions
- The pandas interview round
- SQL to pandas
- XLOOKUP in pandas
- pandas for Excel users
- groupby exercises