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
orNAN
(not a number) in Pandas.
The illustration below shows how the isna
function works in Pandas:
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()
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.
The code snippet below shows how we can use the isna
function in Pandas:
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 in dataframe")print(df.isna())print('\n')print("Null Values in series")print(df["Rank"].isna())
Values that are
None
orNAN
returntrue
. Others returnfalse
.
Free Resources