Pandas: selecting rows whose column value is null / None / nan

Given a pandas dataframe, we have to select rows whose column value is null / None / nan.
Submitted by Pranit Sharma, on November 16, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

While creating a DataFrame or importing a CSV file, there could be some NaN values in the cells. NaN values mean "Not a Number" which generally means that there are some missing values in the cell.

Problem statement

We are given a Dataframe with multiple columns, all these columns contain some integer values and some null/nan values.

Selecting rows whose column value is null / None / nan

Iterating the dataframe row-wise, if any of the columns contain some null/nan value, we need to return that particular row. For this purpose, we will simply filter the dataframe with the help of square brackets and the isna() method.

Square brackets will return all the rows and wherever the condition is satisfied, it will return all the columns.

Let us understand with the help of an example,

Python program to select rows whose column value is null / None / nan

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d= {
    'A':[1,2,3],
    'B':[4,np.nan,5],
    'C':[np.nan,6,7]
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display dataframe
print('Original DataFrame:\n',df,'\n')

# Filtering rows whose column have nan
res = df[df[2].isna()]

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: Pandas: selecting rows whose column value is null / None / nan

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.