Last active
October 13, 2016 15:56
-
-
Save svschannak/3ab92e6d9a3262c812748e2573f581c1 to your computer and use it in GitHub Desktop.
Pandas Find Match for 2 dataframes and add column with right index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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