有一个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映射为(sun、goal和micheal)ABC,以便1替换ABC中未知的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
关于python - 如何匹配和合并两个值完全不同的数据框(单个单词除外)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54418215/