Articles

pandas DataFrame Comparision method - DataFrame.eq()

pandas DataFrame Comparision method - DataFrame.eq()


In this pandas tutorial we will discuss about pandas eq() comparison method.

Introduction

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 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.

 

ExampleCreate Pandas dataframe

In this example, we will create a dataframe with 4 rows and 4 columns with integers data and assign indices through index parameter.

import pandas as pd

#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],

                    'column2':[100,200,300,200],

                   "column3":[23,45,67,43],

                    "column4":[1234,54,67,8]

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

#display the dataframe
print(data)

Output: DataFrame is created below

       column1  column2  column3  column4
one        100      100       23     1234
two        200      200       45       54
three      300      300       67       67
four       200      200       43        8

DataFrame.eq()

This pandas eq() method is used to check the given value is equal to elements present in the dataframe or not.

If present, It will return True at that element position, otherwise it will return false.

Syntax:

dataframe_input.eq(value)

where, dataframe_input is the input pandas dataframe.

 

Example:

In this pandas dataframe comparison example, we will create a dataframe and check the eq() method functionality with two scenarios.

import pandas as pd

#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],

                    'column2':[100,200,300,200],

                   "column3":[23,45,67,43],

                    "column4":[1234,54,67,8]

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

#check the elements present in the dataframe are equal to 200 or not
print(data.eq(200))

print()

#check the elements present in the dataframe are equal to 23 or not
print(data.eq(23))

Output:

In the first output, we observed that in index - two : column 1 and column 2 contains 200. so the result is True and in index - four: column 1 and column 2 contains 200. so the result is True and remaining are set to False.

 

Similarly, in the second output, we will check with value - 23.

       column1  column2  column3  column4
one      False    False    False    False
two       True     True    False    False
three    False    False    False    False
four      True     True    False    False

       column1  column2  column3  column4
one      False    False     True    False
two      False    False    False    False
three    False    False    False    False
four     False    False    False    False

We can also place an operator instead as eq(). The operator used is "==".

Syntax:

dataframe_input==value

Example:

In this pandas dataframe comparison example, we will create a dataframe and check the "==" operator  functionality with two scenarios.

import pandas as pd

#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],

                    'column2':[100,200,300,200],

                   "column3":[23,45,67,43],

                    "column4":[1234,54,67,8]

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

#check the elements present in the dataframe are equal to 200 or not
print(data==200)

print()

#check the elements present in the dataframe are equal to 23 or not
print(data==23)

Output:

In the first output, we observed that in index - two : column 1 and column 2 contains 200. so the result is True and in index - four: column 1 and column 2 contains 200. so the result is True and remaining are set to False.

 

Similarly, in the second output, we will check with value - 23.

       column1  column2  column3  column4
one      False    False    False    False
two       True     True    False    False
three    False    False    False    False
four      True     True    False    False

       column1  column2  column3  column4
one      False    False     True    False
two      False    False    False    False
three    False    False    False    False
four     False    False    False    False

We can also compare with column wise by providing different values.

 

Note - The total values must be equal to column count

Syntax:

dataframe_input.eq([values])

Example:

In this example, we will compare the dataframe with 4 values (since number of columns are 4)

import pandas as pd

#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],

                    'column2':[100,200,300,200],

                   "column3":[23,45,67,43],

                    "column4":[1234,54,67,8]

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

#display actual dataframe
print(data)

print()

#check the elements present in the dataframe are equal to the given values in list or not
print(data.eq([100, 200, 23,8]))

Output:

From the above example, the first value - 100 is compared with first column and set to True whereever 100 is present, the second value - 200 is compared with second column and set to True whereever 200 is present, the third value - 23 is compared with third column and set to True whereever 23 is present and the last value - 8  is compared with forth/last column and set to True whereever 8 is present. Remaining all are set to False.

       column1  column2  column3  column4
one        100      100       23     1234
two        200      200       45       54
three      300      300       67       67
four       200      200       43        8

       column1  column2  column3  column4
one       True    False     True    False
two      False     True    False    False
three    False    False    False    False
four     False     True    False     True

 


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 : Apr 22,2023  
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!