How to Count Missing Values in pandas DataFrames
To count missing values in pandas, use
df.isna() to find missing entries and then sum() to count them. For total missing values per column, use df.isna().sum(), and for the entire DataFrame, use df.isna().sum().sum().Syntax
The main methods to count missing values in pandas are:
df.isna(): Returns a DataFrame of the same shape withTruewhere values are missing (NaN) andFalseotherwise.df.isna().sum(): Counts the number of missing values in each column.df.isna().sum().sum(): Counts the total number of missing values in the entire DataFrame.
python
df.isna() df.isna().sum() df.isna().sum().sum()
Example
This example shows how to create a DataFrame with missing values and count them per column and overall.
python
import pandas as pd import numpy as np data = {'A': [1, 2, np.nan, 4], 'B': [np.nan, 2, 3, 4], 'C': [1, 2, 3, 4]} df = pd.DataFrame(data) # Count missing values per column missing_per_column = df.isna().sum() # Count total missing values in DataFrame total_missing = df.isna().sum().sum() print('Missing values per column:') print(missing_per_column) print('\nTotal missing values in DataFrame:') print(total_missing)
Output
Missing values per column:
A 1
B 1
C 0
dtype: int64
Total missing values in DataFrame:
2
Common Pitfalls
Common mistakes when counting missing values include:
- Using
df.isnull()anddf.isna()interchangeably is fine, but mixing them with other methods likedf.count()without understanding their behavior can cause confusion. - For counting missing values per row, you must specify
axis=1insum(), e.g.,df.isna().sum(axis=1). - Not calling
sum()afterisna()returns a boolean DataFrame, not counts.
python
import pandas as pd import numpy as np data = {'A': [1, np.nan], 'B': [np.nan, 2]} df = pd.DataFrame(data) # Wrong: returns boolean DataFrame, not counts wrong = df.isna() # Right: sum counts missing values per column right = df.isna().sum() print('Wrong output:') print(wrong) print('\nRight output:') print(right)
Output
Wrong output:
A B
0 False True
1 True False
Right output:
A 1
B 1
dtype: int64
Quick Reference
| Method | Description | Example |
|---|---|---|
| df.isna() | Detect missing values (NaN) as True/False | df.isna() |
| df.isna().sum() | Count missing values per column | df.isna().sum() |
| df.isna().sum().sum() | Count total missing values in DataFrame | df.isna().sum().sum() |
| df.isna().sum(axis=1) | Count missing values per row | df.isna().sum(axis=1) |
Key Takeaways
Use df.isna().sum() to count missing values per column in a DataFrame.
Use df.isna().sum().sum() to get the total count of missing values in the entire DataFrame.
Remember to call sum() after isna() to get counts, not just booleans.
To count missing values per row, use sum(axis=1) after isna().
df.isna() and df.isnull() are interchangeable for detecting missing values.