根据其他df的索引创建列

时间:2018-05-01 22:24:17

标签: python pandas join indexing merge

我将团队名称列为一个数据框中的索引,并希望根据匹配标记为Season和Home的列创建另一个df中的列。结果列将包含该团队中该团队的全部总数。

        2012-13 2013-14 2014-15 2015-16 2016-17
Cleveland Cavaliers 1   1   2   1   3
Los Angeles Lakers  2   1   1   1   0
Miami Heat          3   3   2   2   1
Chicago Bulls       2   1   2   2   1

df我想添加到这样的外观

          Date             Visitor  V_PTS                 Home          H_PTS  \
0 2012-10-30 19:00:00  Washington Wizards     84  Cleveland Cavaliers     94   
1 2012-10-30 19:30:00    Dallas Mavericks     99   Los Angeles Lakers     91   
2 2012-10-30 20:00:00      Boston Celtics    107           Miami Heat    120   
3 2012-10-31 19:00:00    Dallas Mavericks     94            Utah Jazz    113   

   Attendance                    Arena                 Location  Capacity  \
0       20562      Quicken Loans Arena          Cleveland, Ohio     20562   
1       18997           Staples Center  Los Angeles, California     18997   
2       20296  American Airlines Arena           Miami, Florida     19600   
3       17634  Vivint Smart Home Arena     Salt Lake City, Utah     18303   

  Yr Arena Opened   Season  
0            1994  2012-13  
1            1992  2012-13  
2            1999  2012-13  
3            1991  2012-13

不太熟悉内部/外部联接,这似乎是我一直在寻找如何解决这个问题

一直在尝试使用这样的代码,但我只是试图让它获得一个专栏所说的H_Allstars和V_Allstars列并未列出所有这些列。

allstar = pd.merge(nba, allstar_df, right_index = True, left_on=['Home'])

                 Date             Visitor  V_PTS                 Home  H_PTS  \
0 2012-10-30 19:00:00  Washington Wizards     84  Cleveland Cavaliers     94   
1 2012-11-02 19:30:00       Chicago Bulls    115  Cleveland Cavaliers     86   
2 2012-11-17 19:30:00    Dallas Mavericks    103  Cleveland Cavaliers     95   
3 2012-11-21 19:00:00  Philadelphia 76ers     83  Cleveland Cavaliers     92   
4 2012-11-27 19:00:00        Phoenix Suns     91  Cleveland Cavaliers     78   

   Attendance                Arena         Location  Capacity Yr Arena Opened  \
0       20562  Quicken Loans Arena  Cleveland, Ohio     20562            1994   
1       20562  Quicken Loans Arena  Cleveland, Ohio     20562            1994   
2       18633  Quicken Loans Arena  Cleveland, Ohio     20562            1994   
3       16743  Quicken Loans Arena  Cleveland, Ohio     20562            1994   
4       13687  Quicken Loans Arena  Cleveland, Ohio     20562            1994   

    Season  2012-13  2013-14  2014-15  2015-16  2016-17  
0  2012-13        1        1        2        1        3  
1  2012-13        1        1        2        1        3  
2  2012-13        1        1        2        1        3  
3  2012-13        1        1        2        1        3  
4  2012-13        1        1        2        1        3 

0 个答案:

没有答案