0

I have a dataframe of transactions between people but its based of their ID number:

df =

First ID   Second ID   Total   Currency
854        938         50      GBP
321        438         30      EUR
756        850         50      USD
etc...

I also have a second df which contains the IDs and the actual names of the people they are linked to.

ID_df =

ID code   Name
321       John
850       David
etc...

I want to join the ID df onto the main dataframe so that i would have the names of the people. Ideally i would like it to look like:

df =

First ID   First name   Second ID   Second name   Total  Currency
854        Steve        938         Mike          60     Eur
etc...
qazwsx123
  • 127
  • 5

1 Answers1

0

What's the issue with 2 back to back joins? (you can make a function out of it in case it becomes 2+)

df = df.merge(id_df,how='left',left_on='first_id',right_on='id_code')\
       .rename(columns={'name':'first_name'})

df = df.merge(id_df,how='left',left_on='second_id',right_on='id_code')\
       .rename(columns={'name':'second_name'})
Partha Mandal
  • 1,152
  • 5
  • 13