What is the isna function in Pandas?

The isna function in Pandas denotes whether null values are present in an object or not. An object can be in the form of a series or dataframe.

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

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

How does isna function work

Syntax

We can use the isna function with a dataframe or series. The syntax for using it with a dataframe is as follows:

pd.DataFrame.isna()

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

pd.Series.isna()

Return value

The isna function returns a mask of the same length as the dataframe or series. isna returns true for every null value present and false for every non-null value.

Example

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

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 in dataframe")
print(df.isna())
print('\n')
print("Null Values in series")
print(df["Rank"].isna())

Values that are None or NAN return true. Others return false.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved