Articles

pandas DataFrame - isnull() and notnull() | isnull & notnull pandas example

pandas DataFrame - isnull() and notnull() | isnull & notnull pandas example


In this pandas tutorial, we will discuss about:

  • isnull in pandas dataframe,
  • pandas isnull example,
  • notnull in pandas dataframe,
  • notnull pandas example,

 

DataFrame in pandas 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.

 

null refers to empty value. In python we can create null 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 None/Null values.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: Dataframe is created

      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

Now lets use the above created dateframe to see use of isnull in pandas dataframe


isnull in pandas dataframe

isnull in pandas is used to check the value is null or not.

If the value is null then it will return True, otherwise False in the DataFrame.

Syntax:

dataframe.isnull()

where, dataframe is the input dataframe

 

Examplepandas isnull example

In this example, we are going to check whether the values are null or not using isnull() 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 null or not
print(data.isnull())

Outputpandas isnull 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

We can see in above pandas isnull example, the values returned is either true or false based on whether value for particular space is null or not. 

 

Lets now see the use of notnull in pandas dataframe.


notnull in pandas dataframe

notnull in pandas dataframe is used to check the value is null or not. If the value is null then it will return False, otherwise True in the DataFrame.

Syntax:

dataframe.notnull()

where, dataframe is the input dataframe

 

Examplenotnull pandas example

In this notnull pandas example, we are going to check whether the values are null or not using notnull() 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 null or not
print(data.notnull())

Outputnotnull pandas 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

In above notnull pandas example, we can see the result is true or false based on respective null or not value.

 

Thus we have seen isnull in pandas dataframenotnull in pandas along with examples.


Pandas

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  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!