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

Question

I want to find the number of NaN in each column of my data.

Answer

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

How to iterate over rows in a DataFrame in Pandas

Renaming column names in Pandas