How do I count the NaN values in a column in pandas DataFrame?

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:

>>> s = pd.Series([1,2,3, np.nan, np.nan])

>>> s.isna().sum()   # or s.isnull().sum() for older pandas versions
2

For several columns, this also works:

>>> df = pd.DataFrame({'a':[1,2,np.nan], 'b':[np.nan,1,np.nan]})

>>> df.isna().sum()
a    1
b    2
dtype: int64

Leave a Comment