Pandas Python Groupby Cummulative Sum Reverse

时间:2017-09-19 14:50:20

标签: python pandas group-by reverse

我找到Pandas groupby cumulative sum并发现它非常有用。但是,我想确定如何计算反向累积和。

该链接表明以下内容。

df.groupby(by=['name','day']).sum().groupby(level=[0]).cumsum()

为了反转总和,我尝试切片数据,但它失败了。

df.groupby(by=['name','day']).ix[::-1, 'no'].sum().groupby(level=[0]).cumsum()


Jack | Monday    | 10 | 90
Jack | Tuesday   | 30 | 80
Jack | Wednesday | 50 | 50
Jill | Monday    | 40 | 80
Jill | Wednesday | 40 | 40 

编辑: 根据反馈,我尝试实现代码并使数据框更大:

import pandas as pd
df = pd.DataFrame(
    {'name': ['Jack', 'Jack', 'Jack', 'Jill', 'Jill'],
     'surname' : ['Jones','Jones','Jones','Smith','Smith'],
     'car' : ['VW','Mazda','VW','Merc','Merc'],
     'country' : ['UK','US','UK','EU','EU'],
     'year' : [1980,1980,1980,1980,1980],
     'day': ['Monday', 'Tuesday','Wednesday','Monday','Wednesday'],
     'date': ['2016-02-31','2016-01-31','2016-01-31','2016-01-31','2016-01-31'],
     'no': [10,30,50,40,40],
     'qty' : [100,500,200,433,222]})

然后我尝试对多个列进行分组,但无法应用分组。

df = df.groupby(by=['name','surname','car','country','year','day','date']).sum().iloc[::-1].groupby(level=[0]).cumsum().iloc[::-1].reset_index()

为什么会这样?我预计杰克琼斯与汽车马自达是一个单独的累积数量杰克琼斯与大众。

1 个答案:

答案 0 :(得分:2)

您可以使用双iloc

df = df.groupby(by=['name','day']).sum().iloc[::-1].groupby(level=[0]).cumsum().iloc[::-1]
print (df)
                no
name day          
Jack Monday     90
     Tuesday    80
     Wednesday  50
Jill Monday     80
     Wednesday  40

对于另一个列解决方案是简化:

df = df.groupby(by=['name','day']).sum()
df['new'] = df.iloc[::-1].groupby(level=[0]).cumsum()
print (df)
                no  new
name day               
Jack Monday     10   90
     Tuesday    30   80
     Wednesday  50   50
Jill Monday     40   80
     Wednesday  40   40

编辑:

第二个groupby需要添加更多关卡时出现问题 - level=[0,1,2]表示先按name,第二surname和第三car级别进行分组。< / p>

df1 = (df.groupby(by=['name','surname','car','country','year','day','date'])
        .sum())
print (df1)
                                                      no  qty
name surname car   country year day       date               
Jack Jones   Mazda US      1980 Tuesday   2016-01-31  30  500
             VW    UK      1980 Monday    2016-02-31  10  100
                                Wednesday 2016-01-31  50  200
Jill Smith   Merc  EU      1980 Monday    2016-01-31  40  433
                                Wednesday 2016-01-31  40  222

df2 = (df.groupby(by=['name','surname','car','country','year','day','date'])
        .sum()
        .iloc[::-1]
        .groupby(level=[0,1,2])
        .cumsum()
        .iloc[::-1]
        .reset_index())
print (df2)
   name surname    car country  year        day        date  no  qty
0  Jack   Jones  Mazda      US  1980    Tuesday  2016-01-31  30  500
1  Jack   Jones     VW      UK  1980     Monday  2016-02-31  60  300
2  Jack   Jones     VW      UK  1980  Wednesday  2016-01-31  50  200
3  Jill   Smith   Merc      EU  1980     Monday  2016-01-31  80  655
4  Jill   Smith   Merc      EU  1980  Wednesday  2016-01-31  40  222

或者可以按名称选择 - 请参阅groupby enhancements in 0.20.1+

df2 = (df.groupby(by=['name','surname','car','country','year','day','date'])
        .sum()
        .iloc[::-1]
        .groupby(['name','surname','car'])
        .cumsum()
        .iloc[::-1]
        .reset_index())
print (df2)

   name surname    car country  year        day        date  no  qty
0  Jack   Jones  Mazda      US  1980    Tuesday  2016-01-31  30  500
1  Jack   Jones     VW      UK  1980     Monday  2016-02-31  60  300
2  Jack   Jones     VW      UK  1980  Wednesday  2016-01-31  50  200
3  Jill   Smith   Merc      EU  1980     Monday  2016-01-31  80  655
4  Jill   Smith   Merc      EU  1980  Wednesday  2016-01-31  40  222