pandas DataFrame - isna() and notna() method | pandas isna & notna example
In this pandas tutorial, we will discuss about:
-
isna in pandas dataframe,
-
pandas isna example,
-
notna in pandas,
-
pandas notna example,
Lets create a pandas dataframe first and then use it in pandas isna example and pandas notna example.
Pandas dataframe is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe, the data can be a dictionary that stores list of values with specified key.
None / NaN refers to Not a number or empty value. In python we can create na by using None keyword or numpy.nan.
Example: create dataframe
In this example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter. We are including some numpy.nan values. We have to import numpy module to create nan values.
import pandas as pd
import numpy as np
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001',np.nan,'c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":[np.nan,"guntur","guntur","guntur"],
"Total Staff":[1200,np.nan,np.nan,670]
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: dataframe is created below
college_id college_name college_address Total Staff
one c-001 vignan university None 1200.0
two None vvit guntur NaN
three c-002 RVR - JC guntur NaN
four c-004 Andhra University guntur 670.0
isna in pandas dataframe
isna() method or isna in pandas dataframe is used to check the value is nan or not. If the value is nan then it will return True, otherwise False in the DataFrame.
Syntax:
dataframe.isna()
where, dataframe is the input dataframe
Example: pandas isna example
In this pandas isna example, we are going to check whether the values are nan or not using isna() method.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001',None,'c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":[None,"guntur","guntur","guntur"],
"Total Staff":[1200,None,None,670]
},index=['one','two','three','four'])
#check the data in the dataframe are nan or not
print(data.isna())
Output: pandas isna example result
college_id college_name college_address Total Staff
one False False True False
two True False False True
three False False False True
four False False False False
Lets now see how to use notna in pandas.
notna in pandas
notna() method or notna in pandas is used to check the value is nan or not. If the value is nan then it will return False, otherwise True in the DataFrame.
Syntax:
dataframe.notna()
where, dataframe is the input dataframe
Example: pandas notna example
In this pandas notna example, we are going to check whether the values are nan or not using notna() method.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001',None,'c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":[None,"guntur","guntur","guntur"],
"Total Staff":[1200,None,None,670]
},index=['one','two','three','four'])
#check the data in the dataframe are nan or not
print(data.notna())
Output: pandas notna example result
college_id college_name college_address Total Staff
one True True False True
two False True True False
three True True True False
four True True True True
Thus we come to end of our chapter on isna in pandas dataframe & notna in pandas along with proper example.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Mar 28,2022