如果两个数据框中的数据相同,则映射列名称

Map column names if data is same in two dataframes

我有两个 pandas 数据帧

df1 = A B C 1 2 3 2 3 4 3 4 5

df2 = X Y Z 1 2 3 2 3 4 3 4 5 我需要根据数据进行映射如果数据相同则映射列名enter code here 输出 = col1 col2 AX 经过 CZ

我找不到任何支持此功能的内置函数,因此只需遍历所有列即可:

pairs = []
for col1 in df1.columns:
    for col2 in df2.columns:
        if df1[col1].equals(df2[col2]):
            pairs.append((col1, col2))

output = pandas.DataFrame(pairs, columns=['col1', 'col2'])