列内的模糊匹配

时间:2019-04-04 13:54:27

标签: python pandas fuzzy fuzzywuzzy

假设我有一个这样的运动清单:

sports=["futball","fitbal","football","tennis","tenis","tenisse","footbal","zennis","ping-pong"]

我想创建一个数据框,如果模糊匹配大于0.5并且不仅仅与自身匹配,则将最接近运动的每个元素匹配。 (我想为此使用函数Fuzzywuzzy.fuzz.ratio(x,y))

结果应类似于:

pd.DataFrame({"sport":sports,"closest_match":["futball","futball","football","tennis","tennis","tennis","futball","tennis","ping-pong"]})

    sport   closest_match
0   futball futball
1   fitbal  futball
2   football football
3   tennis  tennis
4   tenis   tennis
5   tenisse tennis
6   footbal futball
7   zennis  tennis
8   ping-pong ping-pong

谢谢

1 个答案:

答案 0 :(得分:1)

这是使用itertools.combinations的解决方案:

from fuzzywuzzy import fuzz
import pandas as pd
sports = ["futball", "fitbal", "football", "tennis", "tenis", "tenisse", "footbal", "zennis", "ping-pong"]
dist = ([x for x in itertools.combinations(sports, 2) if fuzz.ratio(*x) > 50])

df = pd.DataFrame(dist, columns=["sport","closest"])
df['ratio'] = dist = ([fuzz.ratio(*x) for x in itertools.combinations(sports, 2) if fuzz.ratio(*x) > 50])
print(df)

df = df.groupby(['sport'])[['closest','ratio']].agg('max').reset_index()

输出:

      sport   closest  ratio
0    fitbal  football     77
1  football   footbal     93
2   futball  football     80
3     tenis    zennis     83
4   tenisse    zennis     62
5    tennis    zennis     91