How to count the NaN values in a column in pandas DataFrame How to count the NaN values in a column in pandas DataFrame python python

How to count the NaN values in a column in pandas DataFrame


You can use the isna() method (or it's alias isnull() which is also compatible with older pandas versions < 0.21.0) and then sum to count the NaN values. For one column:

In [1]: s = pd.Series([1,2,3, np.nan, np.nan])In [4]: s.isna().sum()   # or s.isnull().sum() for older pandas versionsOut[4]: 2

For several columns, it also works:

In [5]: df = pd.DataFrame({'a':[1,2,np.nan], 'b':[np.nan,1,np.nan]})In [6]: df.isna().sum()Out[6]:a    1b    2dtype: int64


Lets assume df is a pandas DataFrame.

Then,

df.isnull().sum(axis = 0)

This will give number of NaN values in every column.

If you need, NaN values in every row,

df.isnull().sum(axis = 1)


You could subtract the total length from the count of non-nan values:

count_nan = len(df) - df.count()

You should time it on your data. For small Series got a 3x speed up in comparison with the isnull solution.