Pandas DataFrame Eq Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the eq() method in the Python pandas library to compare values in a DataFrame. The eq() method checks for equal values in the DataFrame and returns a new DataFrame of boolean values, indicating whether the elements are equal or not.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import the pandas library

First, we need to import the pandas library, which will allow us to work with DataFrames.

import pandas as pd

Create a DataFrame

Next, let's create a DataFrame that we will use for comparisons. We will create a DataFrame with two columns: 'Roll no' and 'Marks', and four rows.

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)

Output:

-------The DataFrame is---------
         Roll no  Marks
Saanvi       100     60
Hasini       101     62
Lakshmi      102     65
Kushi        103     59

Compare DataFrame elements with a value

Now, let's use the eq() method to compare the elements of the DataFrame with a specified value. We will compare the DataFrame elements with the value 62.

print("----Find the comparison of the dataframe element with value----")
print(df.eq(62))

Output:

----Find the comparison of the dataframe element with value----
         Roll no  Marks
Saanvi     False  False
Hasini     False   True
Lakshmi    False  False
Kushi      False  False

Compare different columns with different values

We can also compare different columns of the DataFrame with different values. Let's compare the 'Roll no' column with the value 101 and the 'Marks' column with the value 62.

print("----Find the comparison of the dataframe element----")
print(df.eq([101, 62]))

Output:

----Find the comparison of the dataframe element----
         Roll no  Marks
Saanvi     False  False
Hasini      True   True
Lakshmi    False  False
Kushi      False  False

Compare selected column with a value

We can also compare a selected column of the DataFrame with a specific value. Let's compare the 'Marks' column with the value 62.

print("----Find the comparison of the dataframe element----")
print(df["Marks"].eq(62))

Output:

----Find the comparison of the dataframe element----
Saanvi     False
Hasini      True
Lakshmi    False
Kushi      False
Name: Marks, dtype: bool

Compare selected columns with different values

We can compare multiple selected columns of the DataFrame with different values. Let's compare the 'Age' and 'Weight' columns with the values 20 and 60, respectively.

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]))

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

Compare DataFrame columns and add result to a new column

We can compare the columns of the DataFrame and add the result to a new column. Let's compare the 'col1' and 'col2' columns and add the result to a new column called 'Result'.

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)

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

Summary

In this lab, we learned how to use the eq() method in the pandas library to compare values in a DataFrame. By applying the eq() method, we can compare elements with specified values, compare different columns with different values, compare a selected column with a particular value, and compare selected columns with different values. We also learned how to add the result of the comparison to a new column in the DataFrame.

Other Python Tutorials you may like