Python Pandas: How to Check if a Cell Is Empty in a Pandas DataFrame
When working with real-world data in pandas, encountering empty or missing cells is inevitable. Whether you're cleaning survey responses, processing financial records, or preparing data for machine learning, you need to reliably detect and handle these gaps before analysis.
Empty cells in a pandas DataFrame can take several forms:
NaN(Not a Number) - the standard representation of missing data in pandasNone- a Python object indicating absence of a value- Empty strings (
"") - text fields left blank NaT- missing datetime values
In this guide, you'll learn multiple methods to check for empty cells in a pandas DataFrame, from detecting individual missing values to finding entirely empty rows and columns.
Using isnull() to Detect Missing Values
The isnull() method is the most common way to check for missing values in a DataFrame. It returns a DataFrame of the same shape with True for each cell that contains NaN or None, and False for cells with actual data.
import pandas as pd
import numpy as np
df = pd.DataFrame({
'Name': ['Alice', 'Bob', None],
'Age': [25, None, 30],
'Score': [88.5, 92.0, None]
})
print("Original DataFrame:")
print(df)
print("\nEmpty cell check (isnull):")
print(df.isnull())
Output:
Original DataFrame:
Name Age Score
0 Alice 25.0 88.5
1 Bob NaN 92.0
2 None 30.0 NaN
Empty cell check (isnull):
Name Age Score
0 False False False
1 False True False
2 True False True
Every None and NaN value is flagged as True, making it easy to see exactly where data is missing.
Using isna() - An Alias for isnull()
The isna() method produces identical results to isnull(). It's simply an alias with a shorter, more intuitive name.
import pandas as pd
df = pd.DataFrame({
'col1': [1, 2, None],
'col2': [3, None, 5]
})
print(df.isna())
Output:
col1 col2
0 False False
1 False True
2 True False
Both isnull() and isna() are interchangeable. Choose whichever reads better in your code - isna() is slightly more concise and aligns with notna() as its counterpart.
Checking if a Specific Cell Is Empty
To check whether a single cell at a specific row and column is empty, use pd.isnull() with .loc[] or .iloc[]:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'Name': ['Alice', 'Bob', None],
'Age': [25, None, 30]
})
# Check specific cells by label
print(pd.isnull(df.loc[1, 'Age'])) # Row index 1, column 'Age'
print(pd.isnull(df.loc[0, 'Name'])) # Row index 0, column 'Name'
# Check by position
print(pd.isnull(df.iloc[2, 0])) # Row 2, column 0
Output:
True
False
True
Counting Empty Cells
Knowing where cells are empty is useful, but often you also need to know how many are missing. Chain isnull() with sum():
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, np.nan, 3, np.nan],
'B': [np.nan, 2, np.nan, 4],
'C': [np.nan, np.nan, np.nan, np.nan]
})
# Count missing values per column
print("Missing per column:")
print(df.isnull().sum())
# Total missing values in the entire DataFrame
print(f"\nTotal missing cells: {df.isnull().sum().sum()}")
# Missing values as a percentage
print("\nMissing percentage per column:")
print((df.isnull().sum() / len(df) * 100).round(1))
Output:
Missing per column:
A 2
B 2
C 4
dtype: int64
Total missing cells: 8
Missing percentage per column:
A 50.0
B 50.0
C 100.0
dtype: float64
Finding Entirely Empty Rows and Columns
Sometimes you need to identify rows or columns where every cell is empty. Combine isnull() with all():
Finding Empty Rows
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, np.nan, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': [9, 10, np.nan, 12]
})
print("DataFrame:")
print(df)
# Check if ALL values in each row are null (axis=1)
empty_rows = df.isnull().all(axis=1)
print("\nEntirely empty rows:")
print(empty_rows)
Output:
DataFrame:
A B C
0 1.0 5.0 9.0
1 NaN NaN 10.0
2 NaN NaN NaN
3 4.0 8.0 12.0
Entirely empty rows:
0 False
1 False
2 True
3 False
dtype: bool
Row 2 is the only row where all cells are empty.
Finding Empty Columns
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, np.nan, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': [9, 10, np.nan, 12]
})
# Check if ALL values in each column are null (axis=0)
empty_cols = df.isnull().all(axis=0)
print("Entirely empty columns:")
print(empty_cols)
Output:
Entirely empty columns:
A False
B False
C False
dtype: bool
No columns are entirely empty in this example.
Detecting Empty Strings
A common pitfall is that isnull() does not detect empty strings (""). Empty strings are valid Python objects, so pandas treats them as non-null values.
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', '', None],
'City': ['London', 'Paris', '']
})
print("isnull() check:")
print(df.isnull())
Output:
isnull() check:
Name City
0 False False
1 False False
2 True False
Notice that the empty strings ("") in row 1 (Name) and row 2 (City) are marked as False - they are not detected by isnull().
To catch both NaN/None and empty strings, combine checks:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'Name': ['Alice', '', None],
'City': ['London', 'Paris', '']
})
# Detect NaN, None, AND empty strings
truly_empty = df.isnull() | df.eq('')
print("Comprehensive empty check:")
print(truly_empty)
Output:
Comprehensive empty check:
Name City
0 False False
1 True False
2 True True
Now all forms of "empty" are detected.
Filtering Rows with Any Missing Values
To extract rows that contain at least one missing value, use any():
import pandas as pd
import numpy as np
df = pd.DataFrame({
'Product': ['Widget', 'Gadget', 'Gizmo', 'Doohickey'],
'Price': [9.99, None, 14.99, 7.50],
'Stock': [100, 50, None, 200]
})
# Select rows with any missing value
rows_with_missing = df[df.isnull().any(axis=1)]
print("Rows with missing data:")
print(rows_with_missing)
Output:
Rows with missing data:
Product Price Stock
1 Gadget NaN 50.0
2 Gizmo 14.99 NaN
Using notna() to Find Non-Empty Cells
The inverse of isna() is notna(), which returns True for cells that contain actual data:
import pandas as pd
df = pd.DataFrame({
'A': [1, None, 3],
'B': [None, 5, 6]
})
print(df.notna())
Output:
A B
0 True False
1 False True
2 True True
This is especially useful for filtering rows where a specific column has data:
import pandas as pd
df = pd.DataFrame({
'A': [1, None, 3],
'B': [None, 5, 6]
})
# Keep only rows where column 'A' is not empty
filtered = df[df['A'].notna()]
print(filtered)
Output:
A B
0 1.0 NaN
2 3.0 6.0
Quick Reference
| Method | Detects NaN/None | Detects "" | Returns |
|---|---|---|---|
df.isnull() / df.isna() | ✅ | ❌ | DataFrame of booleans |
df.notna() / df.notnull() | ✅ (inverse) | ❌ | DataFrame of booleans |
df.isnull().sum() | ✅ | ❌ | Count per column |
df.isnull().all(axis=1) | ✅ | ❌ | Fully empty rows |
df.isnull().any(axis=1) | ✅ | ❌ | Rows with any missing value |
df.isnull() | df.eq('') | ✅ | ✅ | Both NaN and empty strings |
Conclusion
Checking for empty cells in a pandas DataFrame is a critical first step in any data cleaning workflow. Here's when to use each method:
- Use
isnull()orisna()for a quick boolean map of all missing values across the entire DataFrame. - Use
isnull().sum()to count missing values per column and assess data quality at a glance. - Use
isnull().all()with the appropriateaxisto find entirely empty rows or columns. - Combine
isnull()witheq('')when your data may contain empty strings that should also be treated as missing. - Use
notna()to filter and keep only rows or columns with valid data.
By mastering these methods, you can confidently identify and handle missing data before it impacts your analysis or models.