Skip to content

Instantly share code, notes, and snippets.

@svschannak
Last active October 13, 2016 15:56
Show Gist options
  • Save svschannak/3ab92e6d9a3262c812748e2573f581c1 to your computer and use it in GitHub Desktop.
Save svschannak/3ab92e6d9a3262c812748e2573f581c1 to your computer and use it in GitHub Desktop.
Pandas Find Match for 2 dataframes and add column with right index
def find_match_for_dataframes(df1, df2, cols_to_compare=[], name_of_new_row="found_in_row"):
copy_df2 = df2
df1[name_of_new_row] = ""
for idx, item in df1.iterrows():
for idx_comp, comp_item in copy_df2.iterrows():
# check if all conditions are successful
# returns True if the list is empty
# returns False if the list is not empty because something did not match
matches_all = not [False for col in cols_to_compare if item[col] != comp_item[col]]
if matches_all:
# add index number of match to row
df1.loc[idx, name_of_new_row] = int(idx_comp)
# drop element from comparing element
# so this won't be used twice
copy_df2.drop(copy_df2.index[[idx_comp]])
break
return df1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment