Eq() Function Of Pandas DataFrame Class

Overview:

  • The eq() method of DataFrame class implements the binary operation "Logical Equal To" also called as the "Equality" operation.
  • The eq() method when invoked on a DataFrame instance, checks each element of the instance with the element of another DataFrame instance for equality.
  • The results of the equality tests are returned in a separate DataFrame instance whose elements are Boolean values. A value True indicates the corresponding DataFrame elements were equal in the value. A value False indicates the elements were unequal.

 

Example:

import pandas as pd

 

# Floating point data

floats1 = [(1.1, 1.2, 1.3),

           (0.1, 0.2, 0.3),

           (2.1, 2.2, 2.3)];

 

floats2 = [(0.50, 2.50, 1.0),

           (0.1, 0.2, 0.4),

           (1.5, 2.5, 3.35)];

 

# Floating point data into DataFrames

dataFrame1      = pd.DataFrame(data=floats1);

dataFrame2      = pd.DataFrame(data=floats2);

 

equalityResults = dataFrame1.eq(dataFrame2);

 

# Print the pandas DataFrames and the result of Equality Test

print("Contents of DataFrame1:");

print(dataFrame1);

 

print("Contents of DataFrame2:");

print(dataFrame2);

 

print("DataFrame1.eq(DataFrame2):");

print(equalityResults);

 

Output:

Contents of DataFrame1:

     0    1    2

0  1.1  1.2  1.3

1  0.1  0.2  0.3

2  2.1  2.2  2.3

Contents of DataFrame2:

     0    1     2

0  0.5  2.5  1.00

1  0.1  0.2  0.40

2  1.5  2.5  3.35

DataFrame1.eq(DataFrame2):

       0      1      2

0  False  False  False

1   True   True  False

2  False  False  False  

 


Copyright 2023 © pythontic.com