Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Number of rows with missing data

Tags:

pandas

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.

like image 585
MCG Code Avatar asked Feb 01 '18 17:02

MCG Code


People also ask

How do you get the number of rows with missing data in pandas?

You can extract rows/columns containing missing values from pandas. DataFrame by using the isnull() or isna() method that checks if an element is a missing value.

How do you find the number of missing values in a row in Python?

Since sum() calculate as True=1 and False=0 , you can count the number of missing values in each row and column by calling sum() from the result of isnull() . You can count missing values in each column by default, and in each row with axis=1 .


1 Answers

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
like image 143
Alex Avatar answered Sep 28 '22 15:09

Alex