ValueError: Can only compare identically-labeled DataFrame objects in Python

Dung Do Tien Jul 12 2022 152

Hi guys.

I am trying to create a python app with two dataframes and I want to compare them by using == operator.  You can see the demo here:

import pandas as pd
dfa = pd.DataFrame({'Bodyweight (kg)':[760, 840, 930, 1060, 1200, 560], 'Bench press (kg)':[1350, 1500, 1700, 1400, 1080, 1505]},
index = ['index_1', 'index_2', 'index_3', 'index_4', 'index_5', 'index_6'])

dfb = pd.DataFrame({'Bodyweight (kg)':[756, 840, 903, 1006, 1200, 560], 'Bench press (kg)':[1405, 1020, 1080, 2200, 1075, 1010]},
index = ['index_A', 'index_B', 'index_C', 'index_D', 'index_E', 'index_F'])

print(dfa == dfb)

But when ran this code I got an exception ValueError: Can only compare identically-labeled DataFrame objects. Here is the full exception:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    print(dfa == dfb)
  File "/usr/lib/python3.8/site-packages/pandas/core/ops/__init__.py", line 701, in f
    self, other = _align_method_FRAME(self, other, axis, level=None, flex=False)
  File "/usr/lib/python3.8/site-packages/pandas/core/ops/__init__.py", line 510, in _align_method_FRAME
    raise ValueError(
ValueError: Can only compare identically-labeled DataFrame objects


** Process exited - Return Code: 1 **
Press Enter to exit terminal

Can anyone help me?

Thanks in advance

Have 1 answer(s) found.
  • R

    Roshna t.v. Jul 12 2022

    We can drop the indexes of the DataFrames using the reset_index() method, then we can compare the DataFrames. To drop the indexes, we need to set the parameter drop = True. And then we can use the equals() method to see if all elements are the same in both DataFrame objects. Let’s look at the code:

    import pandas as pd
    dfa = pd.DataFrame({'Bodyweight (kg)':[760, 840, 930, 1060, 1200, 560], 'Bench press (kg)':[1350, 1500, 1700, 1400, 1080, 1505]},
    index = ['index_1', 'index_2', 'index_3', 'index_4', 'index_5', 'index_6'])
    
    dfb = pd.DataFrame({'Bodyweight (kg)':[756, 840, 903, 1006, 1200, 560], 'Bench press (kg)':[1405, 1020, 1080, 2200, 1075, 1010]},
    index = ['index_A', 'index_B', 'index_C', 'index_D', 'index_E', 'index_F'])
    
    dfa = dfa.reset_index(drop=True)
    dfb = dfb.reset_index(drop=True)
    print(dfa == dfb)

    #Output

       Bodyweight (kg)  Bench press (kg)
    0            False             False
    1             True             False
    2            False             False
    3            False             False
    4             True             False
    5             True             False
    
    
    ** Process exited - Return Code: 0 **
    Press Enter to exit terminal

    I hope this answer is helpful to you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close