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