Python 如何匹配和合并两个数据帧,这两个数据帧的值除了一个单词外完全不同?有10行的ABC和22550行的XYZ
有10行的ABC和22550行的XYZ 数据帧ABC值:Python 如何匹配和合并两个数据帧,这两个数据帧的值除了一个单词外完全不同?有10行的ABC和22550行的XYZ,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,有10行的ABC和22550行的XYZ 数据帧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
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
....
....
.....
....
22550
22551 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]))
pat = (r'({})'.format('|'.join(d.keys())))
ABC[1]=ABC[0].str.extract(pat,expand=False).map(d)
print(ABC)
错误:TypeError:序列项16069:应为str实例,找到float
from itertools import chain
abc.loc[:,1] = list(chain(*[xyz.loc[abc[0].str.contains(i),1] for i in xyz[0]]))
错误:索引错误:作为索引器提供的不可对齐的布尔序列(布尔序列的索引和索引对象的索引不匹配
d = dict(zip(XYZ[0], XYZ[1]))
ABC[1] = [next(d.get(y) for y in x.split() if y in d) for x in ABC[0]]
print (ABC)
错误:StopIteration:如果值不匹配,则可以获取默认参数
不匹配
:
d = dict(zip(XYZ[0].str.lower(), XYZ[1]))
ABC[1] = [next(iter(d.get(y) for y in x.lower().split() if y in d),'no match') for x in ABC[0]]
一般解决方案:
import re
XYZ = XYZ.dropna()
d = dict(zip(XYZ[0].str.lower(), XYZ[1]))
for k, v in d.items():
ABC.loc[ABC[0].str.contains(re.escape(k), case=False, na=False), 1] = v
我没有找到匹配项。是因为上下两个大写字母还是间距问题。例如:ABC有iphone xs,XYZ有iphone XS@hukkemaaru-我想是一些数据问题,而不是一些空白?请检查
打印(XYZ[0].tolist())
。也可能是数据不匹配,因为在ABC
中不存在。让我们来看看。