Pandas DataFrame eq() Method
In this tutorial, we will learn the Python pandas DataFrame.eq()
method. It is used to check equal value in the dataframe, element-wise. It returns the DataFrame of bool values consisting true and false. It is equivalent to ==
operator with support to choose axis (rows or columns) and level for comparison.
The below shows the syntax of the DataFrame.eq()
method.
Syntax
DataFrame.eq(other, axis='columns', level=None)
Parameters
other: scalar, sequence, Series, or DataFrame. Any single or multiple element data structure, or list-like object.
axis:{0 or ‘index’, 1 or ‘columns’}, default ‘columns’. Whether to compare by the index (0 or ‘index’) or columns (1 or ‘columns’).
level: int or label. Broadcast across a level, matching Index values on the passed MultiIndex level.
Example: Finding the comparison of the dataframe element with the specified value
By applying the DataFrame.eq()
method on the DataFrame, we can get the comparison with the specified value. The below example shows the same.
import pandas as pd
df = pd.DataFrame({"Roll no": [100, 101, 102, 103],"Marks": [60, 62, 65, 59]},index= ["Saanvi", "Hasini", "Lakshmi", "Kushi"])
print("-------The DataFrame is---------")
print(df)
print("----Find the comparison of the dataframe element with value----")
print(df.eq(62))
Once we run the program we will get the following output.
-------The DataFrame is---------
Roll no Marks
Saanvi 100 60
Hasini 101 62
Lakshmi 102 65
Kushi 103 59
----Find the comparison of the dataframe element with value----
Roll no Marks
Saanvi False False
Hasini False True
Lakshmi False False
Kushi False False
Example 2: Finding the comparison of the dataframe element with the specified value
Here, using the DataFrame.eq()
method, we can compare the DataFrame elements with the certain values. See the below example.
#Comparing different column with different value
import pandas as pd
df = pd.DataFrame({"Roll no": [100, 101, 102, 103],"Marks": [60, 62, 65, 59]},index= ["Saanvi", "Hasini", "Lakshmi", "Kushi"])
print("-------The DataFrame is---------")
print(df)
print("----Find the comparison of the dataframe element----")
print(df.eq([101,62]))
Once we run the program we will get the following output.
-------The DataFrame is---------
Roll no Marks
Saanvi 100 60
Hasini 101 62
Lakshmi 102 65
Kushi 103 59
----Find the comparison of the dataframe element----
Roll no Marks
Saanvi False False
Hasini True True
Lakshmi False False
Kushi False False
Example: Finding the comparison of the dataframe element with the differet values
By using the DataFrame.eq()
method, we can compare the selected column of the DataFrame elements with value. The below example shows the same.
#Comparing selected column with different value
import pandas as pd
df = pd.DataFrame({"Roll no": [100, 101, 102, 103],"Marks": [60, 62, 65, 59]},index= ["Saanvi", "Hasini", "Lakshmi", "Kushi"])
print("-------The DataFrame is---------")
print(df)
print("----Find the comparison of the dataframe element----")
print(df["Marks"].eq(62))
Once we run the program we will get the following output.
-------The DataFrame is---------
Roll no Marks
Saanvi 100 60
Hasini 101 62
Lakshmi 102 65
Kushi 103 59
----Find the comparison of the dataframe element----
Saanvi False
Hasini True
Lakshmi False
Kushi False
Name: Marks, dtype: bool
Example: Finding the comparison of the dataframe element
Using the DataFrame.eq()
method we can compare the selected columns of the DataFrame elements with value. The below example shows the same.
#Comparing selected columns with different value
import pandas as pd
chart = {'Name':['Chetan','yashas','yuvraj'],'Age': [20,25,30],'Height': [155,170,165],'Weight': [59,60,75]}
df = pd.DataFrame(chart)
print("-------The DataFrame is---------")
print(df)
print("----Find the comparison of the dataframe element----")
print(df[["Age", "Weight"]].eq([20,60]))
Once we run the program we will get the following output.
-------The DataFrame is---------
Name Age Height Weight
0 Chetan 20 155 59
1 yashas 25 170 60
2 Yuvraj 30 165 75
----Find the comparison of the dataframe element----
Age Weight
0 True False
1 False True
2 False False
Example: Finding the comparison of the dataframe Element
Using the DataFrame.eq()
method we can compare the columns of the DataFrame and get the result into a dataframe column. The below example shows the same.
#Comparing selected columns with different value
import pandas as pd
df = pd.DataFrame({"col1": [10, 30, 60, 40, 20],"col2": [10, 15, 60, 45, 20]})
print("-------The DataFrame is---------")
print(df)
print("----Find the comparison of the dataframe element----")
df['Result'] = df['col1'].eq(df['col2'])
print(df)
Once we run the program we will get the following output.
-------The DataFrame is---------
col1 col2
0 10 10
1 30 15
2 60 60
3 40 45
4 20 20
----Find the comparison of the dataframe element----
col1 col2 Result
0 10 10 True
1 30 15 False
2 60 60 True
3 40 45 False
4 20 20 True
Conclusion
In this tutorial, we will learn the Python pandas DataFrame.eq()
method. We solved examples and compare the DataFrame elements, columns with certain values and get the DataFrame of bool.