0

I am trying to do a left join between a similar table df1 and df2 as the following:

Table df2:

Year    Team    Elo
1975    ANA     1483.527161
1975    BOS     1528.148559
1976    ARI     1499.237268
1976    LAD     1533.241241
1977    ATL     1490.446315
1978    BAL     1533.906786
1979    BOS     1528.148559
1980    CHC     1473.185062
1981    CHW     1494.119
1981    PIT     1507.463466

"One difference between my real table with this example is that my original df2 table contains about 40 different years and each year contains about 20 different teams. I mean per each year, each team has a different Elo value."

And this table df1 :

Game_ID         Team    Value 1     Value2      Year
HOU197504070    ANA     HOU         ATL         1975
HOU197504080    LAD     LAD         ATL         1975
HOU197504070    BOS     ATL         CHW         1975
HOU197504080    CHC     HOU         ATL         1975

The code I am trying to use is:

df1= df1.join(df2, on=['Year','Team'])

However, I am getting this error:

ValueError: len(left_on) must equal the number of levels in the index of "right"

My expected output is

    Game_ID         Team    Value 1     Value2      Year    Elo
    HOU197504070    ANA     HOU         ATL         1975    1483.527161
    HOU197504080    LAD     LAD         ATL         1975    0
    HOU197504070    BOS     ATL         CHW         1975    1528.148559
    HOU197504080    CHC     HOU         ATL         1975    0

Thanks.

1 Answers1

2

You need a merge, not a join (the latter attempts to merge on index):

df1.merge(df2, on=['Year', 'Team'], how='left').fillna(0)
DYZ
  • 55,249
  • 10
  • 64
  • 93