Pandas DataFrame notnull() Method
In this tutorial, we will learn the Python pandas DataFrame.notnull()
method. This method is used to detect the existing values. It returns a DataFrame consisting of bool values for each element in DataFrame that indicates whether an element is not a null value.
While detecting the existing values, the DataFrame.notnull()
method does not consider the characters such as empty strings ''
or numpy.inf
as null values.
The below is the syntax of the DataFrame.notnull()
method.
Syntax
DataFrame.notnull()
Example 1: Detecting existing values using the DataFrame.notnull()
Method
Here, we are detecting the existing values in the DataFrame using the DataFrame.notnull()
method which returns the DataFrame consisting of bool values for each element in DataFrame that indicates whether an element is not an null
value. If the value is True, it indicates that the element is not a null 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.notnull())
------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 True False True True
1 False True False False
2 True True False True
Example: Detecting missing values using the DataFrame.notnull()
Method
This example is similar to the previous one and the DataFrame.notnull()
method considers the empty strings as normal values. See the below example. In the output for the empty strings and null values, the DataFrame.notnull()
method returns a True
.
#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.notnull())
------The DataFrame is----------
a b
0 0
1 1 None
2 3
---------------------------------
a b
0 True True
1 True False
2 True True
Conclusion:
In this tutorial, we learned the Python pandas DataFrame.notnull()
method. We learned the syntax and we check whether the DataFrame contains the existing values or not using the DataFrame.notnull()
method.