Equality in Pandas DataFrames - Column Order Matters?

后端 未结 8 1955
说谎
说谎 2020-12-13 19:24

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

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 20:08

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

提交回复
热议问题