pandas cumsun - 达到最大值后重置或 ID 更改时重置

pandas cumsun - reset after hiting max or reset when ID change

根据这个问题

Python pandas cumsum() reset after hitting max

我想在达到最大值后重置 cumsum 在 ID 更改时重置。

Transaction_ID  Time            TimeDelta       CumSum[ms]
1              00:00:04.500     00:00:00.000    000
1              00:00:04.600     00:00:00.100    100
1              00:00:04.762     00:00:00.162    262
2              00:00:05.543     00:00:00.781    1043
2              00:00:09.567     00:00:04.024    5067
2              00:00:10.654     00:00:01.087    6154
2              00:00:14.300     00:00:03.646    9800
3              00:00:14.532     00:00:00.232    10032
3              00:00:16.500     00:00:01.968    12000
3             00:00:17.543     00:00:01.043    13043

检查 link 中的函数,并使用 groupby

构建函数
def yourcumsum(x,maxvalue,lastvalue):
    newcum = []
    for row in x:
        thisvalue =  row + lastvalue
        if thisvalue > maxvalue:
            thisvalue = 0
        newcum.append( thisvalue )
        lastvalue = thisvalue
    return newcum


df['new'] = df.TimeDelta.dt.total_seconds()*1000

df['new' = df.groupby('Transaction_ID')['new'].transform(lambda x : yourcumsum(x,5000,0))
0       0.0
1     100.0
2     262.0
3     781.0
4    4805.0
5       0.0
6    3646.0
7     232.0
8    2200.0
9    3243.0
Name: new, dtype: float64