Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
python

What is the isnull function in Pandas?

Hassaan Waqar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The isnull function in Pandas is used to denote whether null values are present in an object or not. An object can be in the form of a series, data frame, or single element.

Null values are represented by None or NAN (not a number) in Pandas.

The illustration below shows how the isnull function works in Pandas:

How does isnull function work

Syntax

We can use the isnull function with a data frame, series, or single element. The syntax for using isnull with a data frame is as follows:

pd.DataFrame.isnull()

The syntax for using isnull with a series is as follows:

pd.Series.isnull()

The syntax for using isnull with a single element is as follows:

pd.isnull(value)

Return value

The isnull function returns a mask of the same length as the data frame or series. It returns true for every null value present and false for every non-null value.

isnull returns a boolean value when used on a single element.

Example

The code snippet below shows how we can use the isnull function in Pandas:

The isnull function has been used on a data frame, series, and individual objects.

import pandas as pd
import numpy as np
# Creating a dataframe
df = pd.DataFrame({'Sports': ['Football', 'Cricket', 'Baseball', 'Basketball',
'Tennis', None, 'Archery', None, 'Boxing'],
'Player': ["Messi", "Afridi", "Chad", "Johnny", "Federer",
"Yong", "Mark", None, "Khan"],
'Rank': [1, 9, 7, np.NAN, 1, 2, 11, np.NAN, 1] })
print("Original Dataframe")
print(df)
print('\n')
print("Null Values")
print('\n')
print("On a Dataframe")
print(df.isnull())
print('\n')
print("On a Series")
print(df["Sports"].isnull())
print('\n')
print("On a single element")
print(df.iloc[[0],[0]].isnull())
print('\n')
print("Other examples on single elements")
print(pd.isnull("Cat"))
print(pd.isnull(np.NAN))

RELATED TAGS

pandas
python

CONTRIBUTOR

Hassaan Waqar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring