Equality in Pandas DataFrames - Column Order Matters?

后端 未结 8 1994
说谎
说谎 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:17

    The most common intent is handled like this:

    def assertFrameEqual(df1, df2, **kwds ):
        """ Assert that two dataframes are equal, ignoring ordering of columns"""
        from pandas.util.testing import assert_frame_equal
        return assert_frame_equal(df1.sort_index(axis=1), df2.sort_index(axis=1), check_names=True, **kwds )
    

    Of course see pandas.util.testing.assert_frame_equal for other parameters you can pass

提交回复
热议问题