Search code examples
pythonpandasdataframesimilarity

Check if two pandas dataframes are equal with mismatching indexes python


I was wondering if it is possible to check the similarity between the two dataframes below. They are the same, however the first and the third rows are flipped. Is there a way to check that these dataframes are the same regardless of the order of the index? Thank you for any help!

enter image description here


Solution

  • You can use merge and then look for a subset of rows that doesn't exist in either dataframe.

    df_a = pd.DataFrame([['a','b','c'], ['c','d','e'], ['e','f','g']], columns=['col1','col2','col3'])
    df_b = pd.DataFrame([['e','f','g'], ['c','d','e'], ['a','b','c']], columns=['col1','col2','col3'])
    df_merged = pd.merge(df_a, df_b, on=df_a.columns.tolist(), how='outer', indicator='Exist')
    print(df_merged[(df_merged['Exist'] != 'both')])