0

Is there a way to find the closest match to a string? The df has 2 independent columns. user_input is a column with 1000 non-emptyrows. Since there are only 20 possible_fruits, possible_fruits has 980 empty rows.

df
    user_input    possible_fruits
     aapl            apple
     applee          pear
     aaaple          banana
...

Expected Output

user_input  closest_match_in_possible_fruits
aapl            apple      
applee          apple      
aaaple          apple      

asd
  • 1,245
  • 5
  • 14
  • Does this help: https://stackoverflow.com/questions/10018679/python-find-closest-string-from-a-list-to-another-string/10018734 – TUNAPRO1234 Oct 26 '20 at 09:40

1 Answers1

1

Try using http://docs.python.org/library/difflib.html#difflib.get_close_matches

difflib.get_close_matches(user_input1, possible_fruits)

For the columns do

for i in df:
   answer = difflib.get_close_matches(df['user_input'][i], df['possible_fruits'])
   print (answer)

Edit:

user_input = df['user_input']
possible_fruits = df['possible_fruits']

for i in user_input:
    answer = difflib.get_close_matches(user_input[i], possible_fruits)
    print(answer)


Oliver Hnat
  • 797
  • 4
  • 19