我有一个这样的数据框:
make model
0 allard K1
1 alllard J2
2 alpine renault A110
3 alpine renualt A310
4 amc (rambler American
5 amc (rambler) Marlin
6 aries 1907
7 ariès 1932
8 austin healey 3000
9 austin-healey Sprite
10 benjamin et benova Type B3
11 benjamin/benova Type P2
12 benjmin/benova Type P3
目标是使第三列的行索引具有最高的模糊比(最接近的模糊匹配)。
如何有效地比较行?
答案 0 :(得分:1)
使用fuzzywuzzy
,并假设make
列的模糊程度应匹配,您可以尝试:
import pandas as pd
from itertools import product
from fuzzywuzzy.fuzz import ratio
df = pd.read_csv('data.csv')
keys = list(set(df['make']))
ratios = pd.DataFrame([{'k1': k1, 'k2': k2, 'ratio': ratio(k1, k2)} for k1, k2 in product(keys, keys) if k1 != k2])
def find_closest(make):
return df[df['make'] == ratios.loc[ratios[ratios['k1'] == make]['ratio'].argmax(), 'k2']].index.values[0]
df['closest_index'] = df['make'].apply(find_closest)
print(df)
数据输出:
make model closest_index
0 allard K1 1
1 alllard J2 0
2 alpine renault A110 3
3 alpine renualt A310 2
4 amc (rambler American 5
5 amc (rambler) Marlin 4
6 aries 1907 7
7 ariès 1932 6
8 austin healey 3000 9
9 austin-healey Sprite 8
10 benjamin et benova Type B3 11
11 benjamin/benova Type P2 12
12 benjmin/benova Type P3 11