In this article we will discuss how to find NaN or missing values in a Dataframe.

Manytimes we create a DataFrame from an exsisting dataset and it might contain some missing values in any column or row.  For every missing value Pandas add NaN at it’s place.

Let’s create a dataframe with missing values i.e.

Contents of the dataframe are,


Now let’s count the number of NaN in this dataframe using dataframe.isnull()

Pandas Dataframe provides a function isnull(), it returns a new dataframe of same size as calling dataframe, it contains only True & False only. With True at the place NaN in original dataframe and False at other places. Let’s call this function on above dataframe dfObj i.e.

It will return a new DataFrame with True & False data i.e.

This contains True at the place NaN in dfObj and False at other places. We are going to use this dataframe to calculate total NaN in original dataframe dfObj.

Count all NaN in a DataFrame (both columns & Rows)

Calling sum() of the DataFrame returned by isnull() will give the count of total NaN in dataframe i.e.

Now suppose we want to count the NaN in each column individually, let’s do that.

Count total NaN at each column in DataFrame

Calling sum() of the DataFrame returned by isnull() will give a series containing data about count of NaN in each column i.e.

Count total NaN at each row in DataFrame

To count the total NaN in each row in dataframe, we need to iterate over each row in dataframe and call sum() on it i.e.

It’s output will be,

Complete example is as follows,



Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.