基于另一个数据框中的多个列创建一个新列

时间:2019-09-20 01:31:26

标签: python-3.x pandas

我有两个数据框。

第一个是个人游戏数据,看起来像这样:

    Team1 Team2  Team1Score   Team2Score   Year
0     a     g        100          90       2019
1     b     h         90         100       2018
2     c     i         95         105       2017
3     d     j        100         115       2018
4     e     k        105         100       2016
5     f     l        120         130       2015
6     a     f        100         115       2015 

第二个是该年度的团队数据,看起来像这样:

    Team Rating   Year
0     a     111    2019
1     g     108    2019
2     j     105    2018
3     l      98    2015
4     b     104    2018
5     f     120    2015
6     a     100    2015

我要合并它们,使其看起来像这样:

    Team1 Team2  Team1Score   Team2Score   Year   Team1Rating  Team2Rating
0     a     g        100          90       2019       111          108
1     b     h         90         100       2018       104          NULL
2     c     i         95         105       2017       NULL         NULL
3     d     j        100         115       2018       NULL         105
4     e     k        105         100       2016       NULL         NULL
5     f     l        120         130       2015       120           98
6     a     f        100         115       2015       100          120

我基本上希望拥有比赛数据的球队得分。我不确定该如何处理以及应该采取什么步骤

1 个答案:

答案 0 :(得分:0)

您可以使用DataFrame.lookup映射df2:

df1['Team1Rating']='NULL'
df1['Team2Rating']='NULL'
cond1=df1['Team1'].isin(df2['Team'])&df1['Year'].isin(df2['Year'])
cond2=df1['Team2'].isin(df2['Team'])&df1['Year'].isin(df2['Year'])
lk=df2.pivot_table(index='Year',columns='Team')
lk.columns=lk.columns.droplevel(0)
rt1=lk.lookup(df1.loc[cond1,'Year'],df1.loc[cond1,'Team1'])
rt2=lk.lookup(df1.loc[cond2,'Year'],df1.loc[cond2,'Team2'])
df1.loc[cond1,'Team1Rating']=rt1
df1.loc[cond2,'Team2Rating']=rt2
print(df1)

  Team1 Team2  Team1Score  Team2Score  Year Team1Rating Team2Rating
0     a     g         100          90  2019         111         108
1     b     h          90         100  2018         104        NULL
2     c     i          95         105  2017        NULL        NULL
3     d     j         100         115  2018        NULL         105
4     e     k         105         100  2016        NULL        NULL
5     f     l         120         130  2015         120          98
6     a     f         100         115  2015         100         120