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.

Selecting rows whose column value is null / None / nan

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.

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

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 code 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:

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

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.