rev20
rev20

Reputation: 23

Cumulative sum in Python without the current row

I need to find the cumulative sum of the 3 previous rows without calculate the current one, here a short example:

ID SUM
A 10
B 5
B 4
B 1
B 2
C 1
C 3

That's the result I want to reach:

ID SUM
A 0
B 0
B 5
B 9
B 10
C 0
C 3

Upvotes: 1

Views: 447

Answers (2)

Andy L.
Andy L.

Reputation: 25239

Use groupby.apply with shift and rolling

df['SUM'] = df.groupby('ID')['SUM'].apply(lambda x: x.shift(fill_value=0)
                                                     .rolling(3,min_periods=1).sum())

Out[50]:
  ID   SUM
0  A   0.0
1  B   0.0
2  B   5.0
3  B   9.0
4  B  10.0
5  C   0.0
6  C   1.0

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150735

You can try:

df['SUM'] = (df.groupby('ID')['SUM']
               .transform(lambda x: x.rolling(4,min_periods=1).sum())
               .sub(df['SUM'])
            )

Output:

  ID  SUM
0  A    0
1  B    0
2  B    5
3  B    9
4  B   10
5  C    0
6  C    1

Upvotes: 1

Related Questions