【发布时间】:2021-08-26 20:35:25
【问题描述】:
我有这个数据集。 df1 = 70,000 行,df2 = ~30 行。我想匹配地址以查看 df2 是否出现在 df1 中,如果出现,我想显示匹配并从 df1 中提取信息以创建新的 df3。有时地址信息有一点偏差。例如(road = rd, street = st, etc )这是一个例子:
df1 =
address unique key (and more columns)
123 nice road Uniquekey1
150 spring drive Uniquekey2
240 happy lane Uniquekey3
80 sad parkway Uniquekey4
etc
df2 =
address (and more columns)
123 nice rd
150 spring dr
240 happy lane
80 sad parkway
etc
这就是 Id 想要的新数据框:
df3=
address(from df2) addressed matched(from df1) unique key(comes from df1) (and more columns)
123 nice rd 123 nice road Uniquekey1
150 spring dr 150 spring drive Uniquekey2
240 happy lane 240 happy lane Uniquekey3
80 sad parkway 80 sad parkway Uniquekey4
etc
这是我迄今为止使用 difflib 尝试过的:
df1['key'] = df1['address']
df2['key'] = df2['address']
df2['key'] = df2['key'].apply(lambda x: difflib.get_close_matches(x, df1['key'], n=1))
this returns what looks like a list, the answer is in []'s so then I convert the df2['key'] into a string using df2['key'] = df2['key'].apply(str)
then I try to merge using df2.merge(df1, on ='key') and no address is matching?
我不确定它可能是什么,但任何帮助将不胜感激。我也在玩fuzzywuzzy 包。
【问题讨论】:
标签: python pandas dataframe fuzzywuzzy difflib