Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

python - I need to get the amount for a certain time window and a certain step

I need to get the amount for a certain time window and a certain step. Below is my dataset.

    TransactionDate PartyId AmountEur
0   2018-04-28  15754387    8.46
1   2018-04-29  15754387    31.82
2   2018-04-30  15754387    1389.43
3   2018-05-01  15754387    134.12
4   2018-05-02  15754387    44.16
5   2018-05-03  15754387    65.62
6   2018-05-04  15754387    20.50
7   2018-05-05  15754387    23.92
8   2018-05-07  15754387    17.41
9   2018-05-08  15754387    10.32

e.g for window=4 and step=1, should be obtained:

    TransactionDate PartyId AmountEur Res
0   2018-04-28  15754387    8.46      8.46 (8.64)
1   2018-04-29  15754387    31.82     40.28 (8.46+31.82)
2   2018-04-30  15754387    1389.43   1429.71 (8.46+31.82+1389.43)
3   2018-05-01  15754387    134.12    1563.83 (8.46+31.82+1389.43+134.12)
4   2018-05-02  15754387    44.16     1599.53 (31.82+1389.43+134.12+44.16)
5   2018-05-03  15754387    65.62     1633.33 (1389.43+134.12+44.16+65.62)
6   2018-05-04  15754387    20.50     264.4  (134.12+44.16+65.62+20.50)
7   2018-05-05  15754387    23.92     154.2 (44.16+65.62+20.50+23.92)
8   2018-05-07  15754387    17.41     127.45 (65.62+20.50+23.92+17.41)
9   2018-05-08  15754387    10.32     72.15 (20.50+23.92+17.41+10.32)

Thanks to everyone who tries to help me!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can do this with a rolling sum:

df['Res'] = df.AmountEur.rolling(4, 1).sum()

check out here for more info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...