Table 1:
ID | Name | Column C | Column D |
---|---|---|---|
1234hj | Bob | 1 | 1 |
nkj234 | Joe | 2 | 2 |
ji3251 | Schmoe | 3 | 3 |
Table 2:
ID | Name |
---|---|
Bob | |
Joe | |
Sam |
I currently have 2 dataframes like so. How do i extract the ID from table 1 and set it as ID in table 2 IF the name matches?
I've tried this code but requires same labelling. (This may not even be correct)
df2['ID'] = np.where(df['Name'] == df2['Name'],
df['Prompt'], df2['ID'])
CodePudding user response:
You can try this:
df2['ID'] = df2['Name'].map(df.set_index('Name')['ID'])
pd.merge
is also an option here.
CodePudding user response:
table2 = pd.merge(table1[['ID','Name']], table2[['Name']], how='right', on='Name')