Signup/Sign In

Pandas DataFrame isnull() Method

In this tutorial, we will learn the Python pandas DataFrame.isnull() method. 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 shows the syntax of the DataFrame.isnull() method.

Syntax

DataFrame.isnull()

Example 1: Detecting Missing Values in Pandas

Here, we are detecting the missing values in the DataFrame using the DataFrame.isnull() 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.isnull())

Once we run the program we will get the following output.


------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.isnull() 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.isnull())

Once we run the program we will get the following output.


------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.isnull() method. We learned the syntax and we check whether the DataFrame contains the null values using the DataFrame.isnull() 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.