Find rows with nan in Pandas

Find rows with nan in Pandas

In this post, we will see how to find rows with nan in Pandas.

What is nan values in Pandas?

A pandas DataFrame can contain a large number of rows and columns. Sometimes, a DataFrame may contain NaN values. Such values indicate that something is not legal and is different from Null which means a value does not exist and is empty.

Such values may make it difficult for data processing. So we usually remove rows that contain NaN values from a DataFrame.

In this article, we will find the rows from a DataFrame that contain such NaN values.

We will work with the following DataFrame where the second row has a NaN value.

Output:

Find rows with NAN in pandas

We can detect NaN values in Python using the isnan() function. This function is present in three modules- math and numpy.

Since we are looking to find rows from a DataFrame, we will use the pandas.isna() function.

This function will return a DataFrame, with True value wherever it encounters NaN or Null values in a DataFrame or a Series object.

For example,

Output:

Note that the above function will return a full DataFrame with True and False values. We can use the any() function to get the rows that contain NaN rows from this DataFrame.

The any() function will return the rows or columns containing True values from a DataFrame. Since we want to find the rows, we will set the axis parameter as 1.

See the following example.

**Output:

Find columns with nan in pandas

We were able to find the rows containing NaN values in a DataFrame. To find the columns, we can set the axis parameter as 0 in the any() function.
Here is an example:

**Output:

Find rows with nan in Pandas using isna and iloc()

We can also use the iloc() method to filter out the rows with NaN values. This function is used to extract rows and columns from the DataFrame based on the index.

See the code below.

**Output:

To deal with Null values, we follow the same procedure and replace the isna() with isnull() function.

That’s about how to find rows with nan in Pandas

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *