-1

I have two data frames df1 and df2. df1 has entries of amounts spent by users and each user can have several entries with different amounts values. The second data frame just holds the information of every users(each user is unique in this data frame). i want to create a new column on df1 that includes the country value of each unique user from df2. Any help will be appreciated

 df1

    name_id      Dept       amt_spent
0   Alex-01      Engineering    5
1   Bob-01       Finance        5
2   Charles-01   HR             10
3   David-01     HR             6
4   Alex-01      Engineering    50

df2
   name_id   Country
0   Alex-01      UK
1   Bob-01       USA
2   Charles-01   GHANA
3   David-01     BRAZIL


Result

    name_id      Dept         amt_spent  Country
0   Alex-01      Engineering  5          UK     
1   Bob-01       Finance      5          USA
2   Charles-01   HR           10         GHANA
3   David-01     HR           6          BRAZIL
4   Alex-01      Engineering  50         UK
GottiJay
  • 31
  • 1
  • 7
  • have looked at the merge function? – MEdwin Jan 16 '19 at 14:50
  • Can use .map since they are unique in the second – ALollz Jan 16 '19 at 14:53
  • import pandas as pd df1 = pd.DataFrame([ ['Alex-01','Engineering', 5], ['Bob-01','Finance', 5], ['Charles-01','HR', 10], ['David-01','HR', 6], ['Alex-01','Engineering', 50]], columns=['name_id', 'Dept', 'amt_spent']) df2 = pd.DataFrame([ ['Alex-01','UK'], ['Bob-01','USA'], ['Charles-01','GHANA'], ['David-01','BRAZIL']], columns=['name_id', 'Country']) df = pd.merge(df1, df2) df – MEdwin Jan 16 '19 at 15:02

1 Answers1

0

This should work:

df = pd.merge(df1, df2)
JE_Muc
  • 5,403
  • 2
  • 26
  • 41