简体   繁体   English

根据来自另一个数据框的 2 个条件创建新的数据框列

[英]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: df输出:

-   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: df1 输出:

  - 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 .我想一个新列添加到df1引用['FT_Team']df在基于'Name''Age'df1

I believe that the new code should look something like a .map;我相信新代码应该看起来像一个 .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最终,我想根据df1 Name AND Age 匹配来自df的足球队

Per Quang Hoang:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 数据框根据另一列的条件创建新行 - Pandas dataframe create new rows based on condition from another column Pandas 根据来自另一个 dataframe 的计数和条件创建新列 - Pandas Create new column based on a count and a condition from another dataframe 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? 根据另一个 dataframe 的匹配结果在 dataframe 中创建新列 - Create new column in a dataframe based on matching result of another dataframe 熊猫:在一个数据框中创建新列,并根据与另一个数据框中的匹配键进行匹配 - Pandas: create new column in one dataframe with values based on matching key from another dataframe 尝试使用Python / pandas基于来自另一个数据帧的列的内部总和来创建新的数据帧 - Trying to create a new dataframe based on internal sums of a column from another dataframe using Python/pandas 根据来自另一个熊猫数据框的列在熊猫数据框中创建新行 - Create new rows in a Pandas Dataframe based on a column from another pandas dataframe 根据来自另一个 DataFrame 的信息,将新列添加到 DataFrame - Add new column to DataFrame based on information from another DataFrame DataFrame 中的新列基于来自另一个 DataFrame 的行和列 - New column in DataFrame based on rows and columns from another DataFrame 根据条件从另一个数据帧的值向数据帧添加新列 - Adding a new column to a dataframe from the values of another dataframe based on a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM