且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在两个数据帧之间找到相等的列

更新时间:2023-11-29 19:52:04

这是利用 广播 以检查两个数据帧之间的相等性并采用 all 以检查所有行匹配的位置.然后我们可以从 np.where(与@piR 的贡献):

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'}