熊猫cumsum由独特的两列

时间:2019-04-07 16:31:23

标签: python pandas cumsum

我正在使用python,并且我希望基于如下所示的结果表来总结两个团队的总体目标:

我将欲望输出放在各列(cumsumlocal和cumsumVisitor)中以进行更好的解释,我想通过seasson来累积每个团队的目标并进行比赛 注意,有两个不同的季节。因此,每个seasson的积聚必须是唯一的。

   Seasson Match    Local   Visitor GoalLocal   goalVisitor _-->cumsumLocal cumsumVisitor
-----------------------------------------------------------------------------------------
    1       1   Machester   Blackburn   2      1    _---->    2       1
    1       1   Leeds       arsenal     2      4    _---->    2       4
    1       2   Blackburn   Leeds       1      3    _---->    3       5
    1       2   Arsenal     Manchester  2      0    _---->    6       2
    1       3   Leeds       Manchester  6      1    _---->    11      3
    1       3   Arsenal     Blackburn   5      0    _---->      
    2       1   Machester   Blackburn   3      1    _---->      
    2       1   Leeds       arsenal     2      0    _---->      
    2       2   Blackburn   Leeds       2      4    _---->      
    2       2   Arsenal     Manchester  1      3    _---->      
    2       3   Leeds       Manchester  2      0    _---->      
    2       3   Arsenal     Blackburn   6      1    _---->      

1 个答案:

答案 0 :(得分:0)

我相信您需要-将_添加到列名称的第一步:

d = {'Local':'Team_Local','Visitor':'Team_Visitor',
     'GoalLocal':'Goal_Local','goalVisitor':'Goal_Visitor'}
df = df.rename(columns=d)
print (df)
    Seasson  Match  Team_Local Team_Visitor  Goal_Local  Goal_Visitor
0         1      1  Manchester    Blackburn           2             1
1         1      1       Leeds      Arsenal           2             4
2         1      2   Blackburn        Leeds           1             3
3         1      2     Arsenal   Manchester           2             0
4         1      3       Leeds   Manchester           6             1
5         1      3     Arsenal    Blackburn           5             0
6         2      1  Manchester    Blackburn           3             1
7         2      1       Leeds      Arsenal           2             0
8         2      2   Blackburn        Leeds           2             4
9         2      2     Arsenal   Manchester           1             3
10        2      3       Leeds   Manchester           2             0
11        2      3     Arsenal    Blackburn           6             1

通过MultiIndex创建split,然后通过stack重塑形状,并通过groupby=+cumsum创建新列,最后通过unstack重塑形状:

df = df.set_index(['Seasson','Match'], append=True)
df.columns = df.columns.str.split('_', expand=True)
df = df.stack()
#pandas 0.24+
df['Cum'] = df.groupby(['Seasson','Team'])['Goal'].cumsum()
#pandas lower
#df['Cum'] = df.reset_index().groupby(['Seasson','Team'])['Goal'].cumsum().values
df = df.unstack().reindex(['Team','Goal','Cum'], axis=1, level=0)
df.columns = df.columns.map('_'.join)
df = df.reset_index(level=0, drop=True).reset_index()

print (df)
    Seasson  Match  Team_Local Team_Visitor  Goal_Local  Goal_Visitor  \
0         1      1  Manchester    Blackburn           2             1   
1         1      1       Leeds      Arsenal           2             4   
2         1      2   Blackburn        Leeds           1             3   
3         1      2     Arsenal   Manchester           2             0   
4         1      3       Leeds   Manchester           6             1   
5         1      3     Arsenal    Blackburn           5             0   
6         2      1  Manchester    Blackburn           3             1   
7         2      1       Leeds      Arsenal           2             0   
8         2      2   Blackburn        Leeds           2             4   
9         2      2     Arsenal   Manchester           1             3   
10        2      3       Leeds   Manchester           2             0   
11        2      3     Arsenal    Blackburn           6             1   

    Cum_Local  Cum_Visitor  
0           2            1  
1           2            4  
2           2            5  
3           6            2  
4          11            3  
5          11            2  
6           3            1  
7           2            0  
8           3            6  
9           1            6  
10          8            6  
11          7            4