MCG Code
MCG Code

Reputation: 1393

Pandas: Number of rows with missing data

How do I find out the total number of rows that have missing data in a Pandas DataFrame? I have tried this:

df.isnull().sum().sum()

But this is for the total missing fields. I need to know how many rows are affected.

Upvotes: 8

Views: 5223

Answers (1)

Alex
Alex

Reputation: 19114

You can use .any. This will return True if any element is True and False otherwise.

df = pd.DataFrame({'a': [0, np.nan, 1], 'b': [np.nan, np.nan, 'c']})
print(df)

outputs

     a    b
0  0.0  NaN
1  NaN  NaN
2  1.0    c

and

df.isnull().any(axis=1).sum()  # returns 2

Upvotes: 11

Related Questions