How to Use isnull in pandas to Detect Missing Data
Use
pandas.isnull() or DataFrame.isnull() to detect missing values (NaN) in your data. It returns a boolean mask showing True where data is missing and False otherwise.Syntax
The isnull() function can be used as pandas.isnull(obj) or as a method on a DataFrame or Series like obj.isnull(). It returns a boolean object of the same shape as obj, where True indicates missing values.
obj: The data to check for missing values. It can be a DataFrame, Series, or array-like.
python
import pandas as pd # Using as a function pd.isnull(obj) # Using as a method on DataFrame or Series obj.isnull()
Example
This example shows how to detect missing values in a DataFrame using isnull(). It returns a DataFrame of booleans where True means the value is missing.
python
import pandas as pd import numpy as np data = {'Name': ['Alice', 'Bob', None, 'David'], 'Age': [25, np.nan, 30, 22], 'City': ['NY', 'LA', 'Chicago', None]} df = pd.DataFrame(data) missing_mask = df.isnull() print(missing_mask)
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 isnull() with checking for empty strings or zeros, which are not considered missing. Also, isnull() detects NaN and None but not other placeholders like 'NA' or 'missing'.
Use isnull() only for standard missing values. For other cases, you may need to replace or convert those placeholders first.
python
import pandas as pd data = {'Value': [0, '', None, 'NA', float('nan')]} df = pd.DataFrame(data) # Wrong: Checking empty string with isnull print(df['Value'].isnull()) # Right: Replace 'NA' and empty string before checking df['Value'].replace(['', 'NA'], pd.NA, inplace=True) print(df['Value'].isnull())
Output
0 False
1 False
2 True
3 False
4 True
Name: Value, dtype: bool
0 False
1 True
2 True
3 True
4 True
Name: Value, dtype: bool
Quick Reference
| Function/Method | Description |
|---|---|
| pandas.isnull(obj) | Detect missing values in any array-like object |
| DataFrame.isnull() | Detect missing values in a DataFrame, returns boolean DataFrame |
| Series.isnull() | Detect missing values in a Series, returns boolean Series |
| DataFrame.notnull() | Opposite of isnull(), shows where data is present |
| Series.notnull() | Opposite of isnull() for Series |
Key Takeaways
Use isnull() to find missing values (NaN or None) in pandas data.
isnull() returns a boolean mask with True where data is missing.
It works as a function or as a method on DataFrame/Series.
isnull() does not detect empty strings or custom missing markers.
Replace custom missing values before using isnull() for accurate results.