I have a question about a fuzzy match.
Here is the function I am trying to write:
JavaScript
x
7
1
def fuzz(x, keys):
2
for i in keys:
3
a = fuzz.ratio(x, keys)
4
return
5
6
dataset['match'] = dataset.col1.apply(fuzz, word=['apple', 'orange', 'banana'])
7
How do I use a for
loop (or other solution) over a list and append matching scores to dataset?
Expected output:
JavaScript
1
10
10
1
col1 match
2
banana 100
3
appl 80
4
oranges 90
5
ba 20
6
.
7
.
8
.
9
.
10
tried to for loop on a list
Advertisement
Answer
JavaScript
1
12
12
1
import fuzzywuzzy
2
from fuzzywuzzy import fuzz
3
4
def fuzz_match(x, keys):
5
ratios = []
6
for i in keys:
7
a = fuzz.ratio(x, i)
8
ratios.append(a)
9
return max(ratios)
10
11
dataset['match'] = dataset.col1.apply(fuzz_match, keys=['apple', 'orange', 'banana'])
12