Count NaN values in Pandas DataFrame

To count NaN values in Pandas DataFrame:

(1) Under a single DataFrame column:

df["column name"].isna().sum()

(2) Under an entire DataFrame:

df.isna().sum().sum()

(3) Across a single DataFrame row:

df.loc[[index value]].isna().sum().sum()

The Example

Suppose you created the following DataFrame that contains NaN values:

import pandas as pd
import numpy as np

data = {
"first_set": [1, 2, 3, 4, 5, np.nan, 6, 7, np.nan, np.nan],
"second_set": ["a", "b", np.nan, np.nan, "c", "d", "e", np.nan, np.nan, "f"],
"third_set": ["aa", np.nan, "bb", "cc", np.nan, np.nan, "dd", np.nan, np.nan, "ee"],
}

df = pd.DataFrame(data)

print(df)

You’ll get this DataFrame with the NaNs:

   first_set   second_set   third_set
0        1.0            a          aa
1        2.0            b         NaN
2        3.0          NaN          bb
3        4.0          NaN          cc
4        5.0            c         NaN
5        NaN            d         NaN
6        6.0            e          dd
7        7.0          NaN         NaN
8        NaN          NaN         NaN
9        NaN            f          ee

Next, you’ll see how to count the NaN values in the above DataFrame for the following 3 cases:

  1. Under a single DataFrame column
  2. Under the entire DataFrame
  3. Across a single DataFrame row

Case 1: Count NaN values under a single DataFrame column

To count NaN values under a single DataFrame column:

df["column name"].isna().sum()

For example, to count the NaN values under the “first_set” column:

import pandas as pd
import numpy as np

data = {
"first_set": [1, 2, 3, 4, 5, np.nan, 6, 7, np.nan, np.nan],
"second_set": ["a", "b", np.nan, np.nan, "c", "d", "e", np.nan, np.nan, "f"],
"third_set": ["aa", np.nan, "bb", "cc", np.nan, np.nan, "dd", np.nan, np.nan, "ee"],
}

df = pd.DataFrame(data)

count_nan = df["first_set"].isna().sum()

print("Count of NaN: " + str(count_nan))

As you can see, there are 3 NaN values under the “first_set” column:

Count of NaN: 3

Case 2: Count NaN values under the entire DataFrame

What if you’d like to count the NaN values under the entire DataFrame?

In that case, use the following syntax to get the total count of NaNs:

df.isna().sum().sum()

For our example:

import pandas as pd
import numpy as np

data = {
"first_set": [1, 2, 3, 4, 5, np.nan, 6, 7, np.nan, np.nan],
"second_set": ["a", "b", np.nan, np.nan, "c", "d", "e", np.nan, np.nan, "f"],
"third_set": ["aa", np.nan, "bb", "cc", np.nan, np.nan, "dd", np.nan, np.nan, "ee"],
}

df = pd.DataFrame(data)

count_nan = df.isna().sum().sum()

print("Count of NaN: " + str(count_nan))

As you can see, the total count of NaNs under the entire DataFrame is 12:

Count of NaN: 12

Case 3: Count NaN values across a single DataFrame row:

To count the NaNs across a single DataFrame row:

df.loc[[index value]].isna().sum().sum()

For instance, to count the NaN values across the row with the index of 7:

import pandas as pd
import numpy as np

data = {
"first_set": [1, 2, 3, 4, 5, np.nan, 6, 7, np.nan, np.nan],
"second_set": ["a", "b", np.nan, np.nan, "c", "d", "e", np.nan, np.nan, "f"],
"third_set": ["aa", np.nan, "bb", "cc", np.nan, np.nan, "dd", np.nan, np.nan, "ee"],
}

df = pd.DataFrame(data)

count_nan = df.loc[[7]].isna().sum().sum()

print("Count of NaN: " + str(count_nan))

The count of NaNs across the row with the index of 7 is two:

Count of NaN: 2

Additional Recourses

Check the Pandas Documentation for additional information about isna.

Leave a Comment