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?

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() functionWas this post helpful? 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 […]

  • 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

    Pandas Loc Multiple Conditions

    💡 Outline Here is the code to select rows by pandas Loc multiple conditions. [crayon-62b8fe5fc3f43024857337/] Here, we are select rows of DataFrame where age is greater than 18 and name is equal to Jay. [crayon-62b8fe5fc3f4a184540932/] The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It […]

  • 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 PythonWas this post helpful? In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we […]

  • 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 PandasWas this post helpful? A dataset has the data neatly arranged in rows and columns. The pandas module in Python allows us to load DataFrames from […]

  • 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.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.