Open In App

Pandas isnull() and notnull() Method

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will delve into the Pandas isnull() and notnull() methods, essential tools provided by the Pandas library for simplifying the import and analysis of data. Pandas prove to be a valuable package for data manipulation, particularly when creating DataFrames from Pandas CSV files. Often, during this process, blank columns are imported as null values, potentially causing complications in subsequent DataFrame operations.

The isnull() and notnull() methods in Pandas address this issue by facilitating the identification and management of NULL values within a data frame DataFrame. These methods offer a means to systematically check for the presence of null values, enabling users to take appropriate actions, such as filtering or replacing, to enhance the overall integrity and usability of the data frame.

Pandas DataFrame isnull() Method

Syntax: pd.isnull(dataframe) or dataframe.isnull()

Parameters: Object to check null values for (DataFrame)

Return Type: DataFrame of Boolean values where True indicates the presence of NaN (Not a Number) values in the specified DataFrame. 

To download the CSV file used, Click Here.

Example: pandas.isnull() Method

In the following example, The Team column is checked for NULL values and a boolean series is returned by the isnull() method which stores True for ever NaN value and False for a Not null value.

Python3




# importing pandas package
import pandas as pd
 
# making data frame from csv file
data = pd.read_csv("/content/employees.csv")
 
# creating bool series True for NaN values
bool_series = pd.isnull(data["Team"])
 
# filtering data
# displaying data only with team = NaN
data[bool_series]


Output

file-llll

Output

As shown in the output image, only the rows having Team=NULL are displayed. 

Count NaN values in Pandas DataFrame

In the following example , the code reads a CSV file (“employees.csv”) into a Pandas DataFrame named “data.” It identifies NaN values in the “Team” column using a boolean series, counts the missing values, and prints the count. Additionally, it displays a subset of the data containing only rows where “Team” values are NaN.

Python3




import pandas as pd
 
# Making a DataFrame from a CSV file
data = pd.read_csv("employees.csv")
 
# Creating a boolean series marking True for NaN values in the "Team" column
bool_series = pd.isnull(data["Team"])
 
# Counting missing values in the "Team" column
missing_values_count = bool_series.sum()
 
# Displaying data only with "Team" values as NaN
filtered_data = data[bool_series]
 
print("Count of missing values in the 'Team' column:", missing_values_count)


Output

Count of missing values in the 'Team' column: 43

Pandas DataFrame notnull() Method

Syntax: pd.notnull(dataframe) or dataframe.notnull()

Parameters: Object to check null values for (DataFrame)

Return Type: DataFrame of Boolean values where False indicates the presence of NaN (Not a Number) values in the specified DataFrame.

Example: notnull() Method

In the following example, the Gender column is checked for NULL values and a boolean series is returned by the notnull() method which stores True for every NON-NULL value and False for a null value.

Python3




# importing pandas package
import pandas as pd
 
# making data frame from csv file
data = pd.read_csv("/content/employees.csv")
 
# creating bool series False for NaN values
bool_series = pd.notnull(data["Gender"])
 
# displayed data only with team = NaN
data[bool_series]


Output

file-22

As shown in the output image, only the rows having some value in Gender are displayed. 

Filter a Pandas Dataframe Based on Null Values

In the following example , below code reads a CSV file (“employees.csv”) into a Pandas DataFrame named “data.” It creates a boolean series, bool_series, marking False for NaN values in the “Gender” column. The code then filters the DataFrame to display only the rows with non-null “Gender” values, creating a subset of data that excludes entries with missing gender information. Finally, it prints and shows this subset of data.

Python3




import pandas as pd
 
# Making a DataFrame from a CSV file
data = pd.read_csv("employees.csv")
 
# Creating a boolean series marking False for NaN values in the "Gender" column
bool_series = pd.notnull(data["Gender"])
 
# Displaying data only with non-null "Gender" values
filtered_data = data[bool_series]
 
print("Data with non-null 'Gender' values:")
print(filtered_data)


Output

kngttgjksdhkjgh

Output



Last Updated : 30 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads