【发布时间】:2019-07-18 20:07:27
【问题描述】:
我有以下数据框:
df = pd.DataFrame(
{'id': [1, 2, 3, 4, 5, 6],
'fruits': ['apple', 'apples', 'orange', 'apple tree', 'oranges', 'mango']
})
id fruits
0 1 apple
1 2 apples
2 3 orange
3 4 apple tree
4 5 oranges
5 6 mango
希望在fruits列中找到模糊字符串,得到一个新的dataframe如下,ratio_score高于80。
如何在 Python 中使用fuzzywuzzy 包做到这一点?谢谢。请注意ratio_score 是一系列作为示例组成的值。
我的解决方案:
df.loc[:,'fruits_copy'] = df['fruits']
df['ratio_score'] = df[['fruits', 'fruits_copy']].apply(lambda row: fuzz.ratio(row['fruits'], row['fruits_copy']), axis=1)
预期结果:
id fruits matched_id matched_fruits ratio_score
0 1 apple 2 apples 95
1 1 apple 4 apple tree 85
2 2 apples 4 apple tree 80
3 3 orange 5 oranges 95
4 6 mango
参考相关:
Fuzzy matching a sorted column with itself using python
Apply fuzzy matching across a dataframe column and save results in a new column
How do I fuzzy match items in a column of an array in python?
Using fuzzywuzzy to create a column of matched results in the data frame
【问题讨论】:
标签: python pandas fuzzy-comparison fuzzywuzzy