Find equal columns between two dataframes

后端 未结 4 1121
迷失自我
迷失自我 2020-12-29 06:49

I have two pandas data frames, a and b:

a1   a2   a3   a4   a5   a6   a7
1    3    4    5    3    4    5
0    2    0           


        
4条回答
  •  我在风中等你
    2020-12-29 07:04

    Here's one way leveraging broadcasting to check for equality between both dataframes and taking all on the result to check where all rows match. Then we can obtain indexing arrays for both dataframe's column names from the result of np.where (with @piR's contribution):

    i, j = np.where((a.values[:,None] == b.values[:,:,None]).all(axis=0))
    dict(zip(a.columns[j], b.columns[i]))
    # {'a7': 'b2', 'a6': 'b3', 'a4': 'b4', 'a2': 'b7'}
    

提交回复
热议问题