简体   繁体   中英

Create new dataframe column based on 2 criteria from another dataframe

I have the following dataframe:

df=pd.DataFrame({'Name':['JOHN','ALLEN','BOB','NIKI','CHARLIE','CHANG'],
              'Age':[35,42,63,29,47,51],
              'Salary_in_1000':[100,93,78,120,64,115],
             'FT_Team':['STEELERS','SEAHAWKS','FALCONS','FALCONS','PATRIOTS','STEELERS']})

df output:

-   Name    Age Salary_in_1000  FT_Team
0   JOHN    35  100 STEELERS
1   ALLEN   42  93  SEAHAWKS
2   BOB 63  78  FALCONS
3   NIKI    29  120 FALCONS
4   CHARLIE 47  64  PATRIOTS
5   CHANG   51  115 STEELERS

And my dataframe that I am trying to complete:

df1=pd.DataFrame({'Name':['JOHN','ALLEN','BOB','NIKI','CHARLIE','CHANG'],
                  'Age':[35,42,63,29,47,51],})

df1 output:

  - Name    Age 
    0   JOHN    35  
    1   ALLEN   42  
    2   BOB 63  78  
    3   NIKI    29  
    4   CHARLIE 47  
    5   CHANG   51

I would like to add a new column to df1 that references ['FT_Team'] from df based upon 'Name' and 'Age' in df1 .

I believe that the new code should look something like a .map; however, I am completely stumped as to what the arguments would be for multiple arguments.

df1['FT_Team] = 

final output:

 -  Name    Age FT_Team
    0   JOHN    35  STEELERS
    1   ALLEN   42  SEAHAWKS
    2   BOB 63  FALCONS
    3   NIKI    29  FALCONS
    4   CHARLIE 47  PATRIOTS
    5   CHANG   51  STEELERS

Ultimately, I would like to match the football team from df based upon Name AND Age in df1

Per Quang Hoang:

df1=df1.merge(df[['Name','Age','FT_Team']], on=['Name','Age'], how='left')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM