Facebook Page Twitter Page LinkedIn Page
× Pandas Tutorial Pandas Resources


The Pandas eq() function is used to get equal to of dataframe and argument, element-wise (binary operator eq). It is equivalent to dataframe == other, but with support to choose axis (rows or columns) and level for comparison. The syntax for using this function is given below:

Syntax

DataFrame.eq(other, axis='columns', level=None)

Parameters

other Required. Specify any single or multiple element data structure, or list-like object.
axis Optional. Specify whether to compare by the index (0 or 'index') or columns (1 or 'columns').
level Optional. Specify int or label to broadcast across a level, matching Index values on the passed MultiIndex level. Default is None.

Return Value

Returns the result of the comparison.

Example: Using eq() on whole DataFrame

In the below example, a DataFrame df is created. The eq() function is used to get the result of comparing for equal to of the whole data frame with given scalar value.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#comparing for equal to for all
#entries of the DataFrame by 4
print("\ndf.eq(4) returns:")
print(df.eq(4))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.eq(4) returns:
       Bonus  Salary
John   False   False
Marry  False   False
Sam    False   False
Jo      True   False

Example: Comparing different column with different value

Different column can be compared with different scalar value by providing other argument as a list. Consider the following example:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#comparing all entries of Bonus column by 4
#comparing all entries of Salary column by 62
print("\ndf.eq([4,62]) returns:")
print(df.eq([4,62]))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.eq([4,62]) returns:
       Bonus  Salary
John   False   False
Marry  False    True
Sam    False   False
Jo      True   False

Example: Using eq() on selected columns

Instead of whole data frame, the eq() function can be applied on selected columns. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Last Salary": [58, 60, 63, 57],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#comparing all entries of Salary column by 62
print("\ndf['Salary'].eq(62) returns:")
print(df["Salary"].eq(62))

#comparing all entries of Bonus column by 4
#comparing all entries of Salary column by 62
print("\ndf[['Salary', 'Bonus']].eq([62,4]) returns:")
print(df[["Salary", "Bonus"]].eq([62,4]))

The output of the above code will be:

The DataFrame is:
       Bonus  Last Salary  Salary
John       5           58      60
Marry      3           60      62
Sam        2           63      65
Jo         4           57      59

df['Salary'].eq(62) returns:
John     False
Marry     True
Sam      False
Jo       False
Name: Salary, dtype: bool

df[['Salary', 'Bonus']].eq([62,4]) returns:
       Salary  Bonus
John    False  False
Marry    True  False
Sam     False  False
Jo      False   True

Example: Using eq() on columns of a DataDrame

The eq() function can be applied in a data frame to get the result of comparing for equal to of two series/column element-wise. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "col1": [10, 20, 30, 40, 50],
  "col2": [10, 15, 30, 45, 55]
})

print("The DataFrame is:")
print(df)

#calculating 'col1' == 'col2'
df['Result'] = df['col1'].eq(df['col2'])

print("\nThe DataFrame is:")
print(df)

The output of the above code will be:

The DataFrame is:
   col1  col2
0    10    10
1    20    15
2    30    30
3    40    45
4    50    55

The DataFrame is:
   col1  col2  Result
0    10    10    True
1    20    15   False
2    30    30    True
3    40    45   False
4    50    55   False

❮ Pandas DataFrame - Functions