As part of a unit test, I need to test two DataFrames for equality. The order of the columns in the DataFrames is not important to me. However, it seems to matter to Panda
When working with dataframes containing python objects such as tuples and lists df.eq(df2)
and df == df2
will not suffice. Even if a the same cells in each dataframes contain the same object, such as (0, 0)
, the equality comparison will result to False
. To get around this, convert all columns to strings before comparison:
df.apply(lambda x: x.astype(str)).eq(df2.apply(lambda x: x.astype(str)))