How to Use isna in pandas to Detect Missing Data
Use the
isna() function in pandas to detect missing values (NaN) in a DataFrame or Series. It returns a boolean object of the same shape, where True indicates a missing value and False means data is present.Syntax
The isna() function can be called on a pandas DataFrame or Series. It returns a boolean object showing True for missing values and False otherwise.
DataFrame.isna(): Checks each cell in the DataFrame.Series.isna(): Checks each element in the Series.
python
DataFrame.isna() Series.isna()
Example
This example shows how to use isna() on a DataFrame to find missing values. The output is a DataFrame of booleans where True marks missing data.
python
import pandas as pd import numpy as np data = {'Name': ['Alice', 'Bob', None, 'David'], 'Age': [25, np.nan, 30, 22], 'City': ['New York', 'Los Angeles', 'Chicago', None]} df = pd.DataFrame(data) missing = df.isna() print(missing)
Output
Name Age City
0 False False False
1 False True False
2 True False False
3 False False True
Common Pitfalls
One common mistake is confusing isna() with isnull(). They are actually the same in pandas, so either works. Another pitfall is expecting isna() to remove missing values; it only detects them.
Also, calling isna() on a non-pandas object will cause an error.
python
import pandas as pd s = pd.Series([1, None, 3]) # Wrong: expecting isna() to drop missing values print(s.isna()) # Shows True/False, does NOT drop # Right: use dropna() to remove missing values print(s.dropna())
Output
0 False
1 True
2 False
dtype: bool
0 1.0
2 3.0
dtype: float64
Quick Reference
| Function | Description |
|---|---|
| isna() | Returns boolean mask of missing values (NaN) |
| isnull() | Alias for isna(), same behavior |
| notna() | Returns boolean mask of non-missing values |
| dropna() | Removes missing values from data |
Key Takeaways
Use pandas isna() to find missing values; it returns True where data is missing.
isna() works on both DataFrames and Series, giving a boolean mask of missing data.
isna() does not remove missing values; use dropna() to remove them.
isna() and isnull() are interchangeable in pandas.
Always call isna() on pandas objects to avoid errors.