0

I have two dataframes as follows :

item_id   name     item_id label
-------------      ------------- 
11         a        11       1 
11         b        12       2 
12         c        13       3
13         d        14       4 
14         e
14         e 

I would like to assign the values of label column to the column id on the left dataframe. I tried using the merge but I guess I made a mistake while joining and not getting the expected result.

Expected result:

item_id    name    label
------------------------
11         a         1
11         a         1 
12         b         2
13         c         3
14         d         4
14         d         4
Sam
  • 352
  • 2
  • 4
  • 22
  • 1
    Is `id` a column of the dataframes or their index? – Ric S Mar 25 '21 at 15:43
  • it is a column. I should have changed the name of the column. Consider it as a `item_cd` – Sam Mar 25 '21 at 15:44
  • 1
    Please check out the duplicated answer. In case it does not respond to your needs, reopen your question with more details – Ric S Mar 25 '21 at 15:46
  • I have checked the same duplicated answer. But it did not work for me. – Sam Mar 25 '21 at 15:47
  • @RicS Thanks for your reply. I have updated the question. – Sam Mar 25 '21 at 15:53
  • `df1.merge(df2, on='item_id', how='left')` should work just fine – Ric S Mar 25 '21 at 16:11
  • @RicS Somehow the left merge removes the duplicates. I would like to have all shown in the expected results – Sam Mar 26 '21 at 03:35
  • For how the left join it's constructed, it's impossible that it removes the duplicates. Check out if there is something wrong in your data, otherwise reopen the question with more details or your exact dataframe (if it is possible to share it) – Ric S Mar 26 '21 at 08:02
  • Thanks @RicS. I have created a separate post with all the details. https://stackoverflow.com/questions/66811137/mapping-values-from-one-column-to-the-values-from-another-column-in-a-pandas-dat/66812466#66812466 – Sam Mar 26 '21 at 08:06

0 Answers0