Skip to content

Instantly share code, notes, and snippets.

@jhconning
Created June 2, 2016 02:27
Show Gist options
  • Save jhconning/5fe77300e351cad6cc81226fc19b23a4 to your computer and use it in GitHub Desktop.
Save jhconning/5fe77300e351cad6cc81226fc19b23a4 to your computer and use it in GitHub Desktop.
#df is the original dataframe with a list of names you want to prevail
#dfF is the dataframe with Names that can be matched only fuzzily
#For each Name in df the code finds the most likely match from the dfF and saves that name
#We then merge on that new key 'Name_r'
#some code is to cover the event of no match (perhaps b/c df has names not in dfF)
# From http://stackoverflow.com/questions/13636848/is-it-possible-to-do-fuzzy-match-merge-with-python-pandas
# http://stackoverflow.com/questions/36557722/python-pandas-difflib-throws-list-index-out-of-range-error
import difflib
df['Name_r'] = df.Name_x.map(lambda x: (difflib.get_close_matches(x, dfF.Name)[:1] or [None])[0])
@filipposebastio
Copy link

hi there!

I get the error IndexError: list index out of range because the dataframes are not the same length.. any suggestion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment