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
orNAN
(not a number) in Pandas.
The illustration below shows how the isnull
function works in Pandas:
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)
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.
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 pdimport numpy as np# Creating a dataframedf = 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))
Free Resources