0

I have two dataframes.

The first is individual game data, which looks similar to this:

    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 

The second is team data for the year, which looks similar to this:

    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

I want to merge them so that it looks like this:

    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

I basically want to have the team ratings with the game data. I'm not sure how to approach this and what steps to take

1 Answers1

0

You can use DataFrame.lookup to map 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
ansev
  • 30,322
  • 5
  • 17
  • 31