Signup/Sign In
PUBLISHED ON: MARCH 18, 2021

Pandas DataFrame isna() Method

In this tutorial, we will learn the Python pandas DataFrame.isna() method. This method can be used to detects the missing values. When this method applied to the DataFrame, it returns the DataFrame of the boolean values. If the resultant DataFrame consists of the True, it indicates that the element is a null value and if it is False it indicates that the element is not a null value. This method does not consider characters such as empty strings '' or numpy.inf as null values.

The below is the syntax of the DataFrame.isna() method.

Syntax

DataFrame.isna()

Example 1: Detecting missing values in Pandas

Here, we are detecting the missing values in the DataFrame using the DataFrame.isna() method which returns the DataFrame consisting of bool values for each element in DataFrame that indicates whether an element is an NA value. See the below example.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating the DataFrame
df = pd.DataFrame([(0.0, np.nan, -1.0, 1.0),(np.nan, 2.0, np.nan, np.nan),(2.0, 3.0, np.nan, 9.0),],columns=list('abcd'))
print("------The DataFrame is----------")
print(df)
print("---------------------------------")
print(df.isna())


------The DataFrame is----------
a b c d
0 0.0 NaN -1.0 1.0
1 NaN 2.0 NaN NaN
2 2.0 3.0 NaN 9.0
---------------------------------
a b c d
0 False True False False
1 True False True True
2 False False True False

Example 2: Detecting missing values in Pandas

This example is similar to the previous one and the DataFrame.isna() method does not consider the empty strings as NA values. See the below example.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating the DataFrame
df = pd.DataFrame({'a':[0,1,''],'b':['',None,3]})
print("------The DataFrame is----------")
print(df)
print("---------------------------------")
print(df.isna())


------The DataFrame is----------
a b
0 0
1 1 None
2 3
---------------------------------
a b
0 False False
1 False True
2 False False

Conclusion

In this tutorial, we learned the Python pandas DataFrame.isna() method. We learned the syntax and we check whether the DataFrame contains the null values using the DataFrame.isna() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.