如何匹配和合并两个具有完全不同的值的数据帧,除了一个单词?

有一个有价值的数据框ABC

        0                        1           2
0   sun is rising         |  UNKNOWN    | 1465465
1   micheal has arrived   |   UNKNOWN   | 324654
2   goal has been scored | UNKNOWN     | 547854

和其他有价值的XYZ

    0         1 
0 sun       | password1
1 goal      | password2
2 micheal   | password3

如何将XYZ与(太阳,目标和米歇尔)ABC进行映射,以便1用密码替换ABC中的UNKNOWN 1

输出我需要

    0                        1           2
0  sun is rising         |  password1    | 1465465
1   micheal has arrived  |   password3   | 324654
2   goal has been scored| password2     | 547854

最佳答案

d = dict(zip(XYZ[0],XYZ[1]))
#{'sun': 'password1', 'goal': 'password2', 'micheal': 'password3'}
pat = (r'({})'.format('|'.join(d.keys())))
ABC[1]=ABC[0].str.extract(pat,expand=False).map(d)
print(ABC)

          0                  1         2
0  sun is rising         password1  1465465
1  micheal has arrived  password3   324654
2  goal has been scored  password2   547854
点赞