Count the Number of NaN in Pandas Dataframes

This tutorial uses 2 examples to show how to count the number of NaN in Pandas dataframes.

Method 1: count the number of NaN by columns:

df.isnull().sum()

Method 2: count the number of NaN in the whole dataframe:

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


Example for Method 1

The following counts the number of NaN by columns using df.isnull().sum().

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 500],
'Col_2': [np.nan, 30, 100, 88, 55],
'Col_3': [88, 87, 79, 88, 55]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# count the number of NaN by columns
df.isnull().sum()

The following is the output summarizing the number of NaN by columns in the dataframe. The first column has 2 NaN; second column has 1 NaN; the third column has 0 NaN.

Dataframe with NaN: 
    Col_1  Col_2  Col_3
0  100.0    NaN     88
1    NaN   30.0     87
2  200.0  100.0     79
3    NaN   88.0     88
4  500.0   55.0     55

Col_1    2
Col_2    1
Col_3    0
dtype: int64

Example for Method 2

The following counts the number of NaN by columns using df.isnull().sum().sum().

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 500],
'Col_2': [np.nan, 30, 100, 88, 55],
'Col_3': [88, 87, 79, 88, 55]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# counts the number of NaN in the whole dataframe
df.isnull().sum().sum()

The following is the output summarizing the total number of NaN in the whole dataframe. In particular, there are 3 NaN in the whold dataframe.

Dataframe with NaN: 
    Col_1  Col_2  Col_3
0  100.0    NaN     88
1    NaN   30.0     87
2  200.0  100.0     79
3    NaN   88.0     88
4  500.0   55.0     55

3

Further Reading