Equality in Pandas DataFrames - Column Order Matters?

后端 未结 8 2002
说谎
说谎 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 19:55

    Probably you may need function to compare DataFrames ignoring both row and column order? Only requirement is to have some unique column to use it as index.

    f1 = pd.DataFrame([
        {"id": 1, "foo": "1", "bar": None},
        {"id": 2, "foo": "2", "bar": 2},
        {"id": 3, "foo": "3", "bar": 3},
        {"id": 4, "foo": "4", "bar": 4}
    ])
    f2 = pd.DataFrame([
        {"id": 3, "foo": "3", "bar": 3},
        {"id": 1, "bar": None, "foo": "1",},
        {"id": 2, "foo": "2", "bar": 2},
        {"id": 4, "foo": "4", "bar": 4}
    ])
    
    def comparable(df, index_col='id'):
        return df.fillna(value=0).set_index(index_col).to_dict('index')
    
    comparable(f1) == comparable (f2)  # returns True
    

提交回复
热议问题