I try compare each row with all rows in pandas DF through fuzzywuzzy.fuzzy.partial_ratio() >= 85 and write results in list for each row.
in: df = pd.DataFrame( {'id':[1, 2, 3, 4, 5, 6], 'name':['dog', 'cat', 'mad cat', 'good dog', 'bad dog', 'chicken']})
use pandas function with fuzzywuzzy library get result:
out:
id name match_id_list
1 dog [4, 5]
2 cat [3, ]
3 mad cat [2, ]
4 good dog [1, 5]
5 bad dog [1, 4]
6 chicken []
But I don't understand how get this.
The first step would be to find the indices that match the condition for a given
name
. Sincepartial_ratio
only takes strings, weapply
it to the dataframe:We can then use
enumerate
and list comprehension to generate the list oftrue
indices in the boolean array:Let's put all this inside a function:
We can now apply the function to the entire dataframe: