Python Pandas 数据框中行的 FuzzyWuzzy 得分
Python FuzzyWuzzy Score on Row in Pandas Dataframe
我想遍历 Pandas 数据框并只为每个行对(不是所有组合)获得 fuzz.ratio 分数。我的数据框如下所示:
Acct_Owner, 地址, 地址2
0,Name1,NaN,自由街 33 号
1, Name2, 330 N Wabash Ave Ste 39300, 330 North Wabash Avenue Suite 39300
存在缺失值,因此我使用 "try:" 跳过缺失值行。下面是当前的 for 循环:
for row in df_high_scores.index:
k1 = df_high_scores.get_value(row, 'Address')
k2 = df_high_scores.get_value(row, 'Address2')
try:
df_high_scores['Address_Score'] = fuzz.ratio(k1, k2)
except:
None
结果显示所有行的得分相同。 希望找出循环未遍历并对每一行评分的原因。 感谢阅读...
分配需要使用带索引的正确行。
df_high_scores.loc[row, 'Address_Score'] = fuzz.ratio(k1, k2)
执行此操作而不是迭代行的更好方法是:
df_high_scores['Address_Score'] = df_high_scores.apply(lambda x : fuzz.ratio(x.Address, x.Address2), axis=1)
apply 对于大型数组实际上很慢。查找模糊以查看是否可以将 numpy 数组或 pandas 系列作为输入。