I've got a pandas df where I've already matched the name to the ID, but there are some IDs that don't have a name. For those, I want to go back to the mapping file and search the 'alternative_ID_list' column and see if there is a match with a corresponding name (ignore the decimal). I also tried something (check very bottom) but it is taking way to long to run.
current df
name ID
0 joe USER1
3 mary USER2
5 USER3 USER3
8 USER4 USER4
9 USER5 USER5
9 USER6 USER6
bad_matches=[3, 4, 5, 6]
mapping_df
name ID alternative_ID_list
0 joe USER1 USER213.32
3 mary USER2 USER643.11
5 sam USER98 USER31.5
7 jack USER992 USER4.2
8 rick USER902 USER5.6, USER321.1
9 john USER979 USER6.8, USER987.9
10 jay USER980 USER479.2, USER989.0
#use mapping_df to find the bad_match_IDs (take the first match found if multiple rows for one bad_match_id)
desired
name ID
0 joe USER1
3 mary USER2
5 USER3 USER3
7 jack USER4
8 rick USER5
9 john USER6
what i've tried (takes way too long): (df2=current_df)
df2=current_df
bad_matches = df2['ID'].loc[df2['name'].isna()]
mapping_df.alternative_ID_list = mapping_df. alternative_ID_list.str.split(',')
mapping_df = mapping_df.explode('alternative_ID_list')
mapping_df. alternative_ID_list = mapping_df. alternative_ID_list.astype(str).str.split('.').str[0].astype(str)
df3 = mapping_df.loc[lambda x: x['alternative_ID_list'].isin(bad_matches)]
df2['name'] = df2['ID'].map(df3.set_index('alternative_ID_list')['name']).fillna(df2['name'])