Counting number of rows with missing values in Pandas DataFrame
Start your free 7-days trial now!
At least one missing value
Consider the following DataFrame:
df
A B Ca NaN 4 NaNb 3.0 5 7.0c NaN 6 8.0
Solution
To count the number of rows that contain at least one missing value:
Explanation
We first use isna() method to get a DataFrame of booleans where True indicates the presence of NaN:
A B Ca True False Trueb False False Falsec True False False
We then use any(axis=1), which returns a Series of booleans where True indicates a row with at least one True:
In Pandas, True is internally represented as a 1, while False as a 0. Therefore, taking the sum of this Series will return the number of rows with at least one missing value:
With all missing values
Consider the following DataFrame:
df
A Ba NaN 3.0b NaN NaN
Solution
To get the number of rows with all missing values:
Explanation
Once again, we first use isna() to get a DataFrame of booleans where True indicates the presence of NaN:
A Ba True Falseb True True
Next, we use all(axis=1) to get a Series of booleans where True indicates a row with all Trues:
In Pandas, True is internally represented as a 1, while False as a 0, so taking the summation tells us the number of rows with all missing column values: