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

Related Posts

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

  • 05 October

    Pandas replace values in column

    Table of ContentsUsing the loc() function to replace values in column of pandas DataFrameUsing the iloc() function to to replace values in column of pandas DataFrameUsing the map() function to replace values of a column in a pandas DataFrameUsing the replace() function to replace values in column of pandas DataFrameUsing the where() function to replace […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Select rows by multiple conditions using loc in Pandas

    The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels. We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in Python In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we may want to […]

  • Read text file in Pandas
    28 July

    Read text file in Pandas

    Table of ContentsUsing the read_csv() function to read text files in PandasUsing the read_table() function to read text files in PandasUsing the read_fwf() function to read text files in Pandas A dataset has the data neatly arranged in rows and columns. The pandas module in Python allows us to load DataFrames from external files and […]

  • Copy DataFrame in Python
    10 July

    Copy DataFrame in Pandas

    This articles provide different ways to copy DataFrame in Pandas.

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.