0

Imagine we have the following 2 pandas tables:

employee_id  country 
1            Sweden
2            Denmark
....
45           Germany

In another table I have the employee_id and want to create a new column where the country for the employee_ID is matched from the other table. Note that in this table the same employee_id can occur throughout multiple rows.

employee_id year salary 
1           2017  45000
2           2017  50000
1           2018  46000
....

How do I create the extra column on the last table using the information from the first table?

I was trying a code like:

df.loc[df.isin(df1.employee_id), 'country'] =  df1.loc[df1.employee_id.isin(df.id), 'country']

However that did not work.

MathiasRa
  • 825
  • 2
  • 12
  • 24
  • Check out `map` – cs95 Mar 05 '19 at 21:56
  • Isnt this simple `merge`? @coldspeed – Erfan Mar 05 '19 at 22:02
  • @Erfan There are multiple ways of solving this, and `merge` would be one of them, yes. I would recommend `map` in this instance, however, because only one column needs merging. See [this answer](https://stackoverflow.com/questions/53645882/pandas-merging-101), specifically the section on "Merging only a single column from one of the DataFrames". – cs95 Mar 05 '19 at 22:12
  • Makes sense, thanks. @coldspeed – Erfan Mar 05 '19 at 22:17

0 Answers0