Praveen Gupta Sanka
Praveen Gupta Sanka

Reputation: 631

Rolling sum windowed for every ID individually

In the following dataframe, I need rolling sum of X for the last two 'Time' periods for every ID.

df = pd.DataFrame({'ID':[1000, 1000, 1000, 2000, 2000, 2000, 3000,3000,3000],
              'Time':[1,2,3,1,2,3,1,2,3],
              'X':[101, 201, 123, 234, 222, 333, 444, 0, 0 ]})

I tried the following code, but it resulted rolling sum carried over from customer ID '1000' to '2000' and so on.

df[['X']].rolling(2).sum()

Upvotes: 0

Views: 41

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

IIUC:

In [190]: df['new'] = df.groupby('ID', as_index=False)['X'].rolling(2).sum().reset_index(level=0, drop=True)

In [191]: df
Out[191]:
     ID  Time    X    new
0  1000     1  101    NaN
1  1000     2  201  302.0
2  1000     3  123  324.0
3  2000     1  234    NaN
4  2000     2  222  456.0
5  2000     3  333  555.0
6  3000     1  444    NaN
7  3000     2    0  444.0
8  3000     3    0    0.0

Upvotes: 1

Related Questions