I am trying to do a rolling sum of data frame. Sample of data frame:
cdateint severity cnt_alerts
0 20200511 1 48
1 20200511 2 89
2 20200511 3 5
3 20200511 4 1
4 20200512 1 48
5 20200512 2 69
6 20200512 3 1
7 20200512 4 0
8 20200513 1 47
9 20200513 2 62
10 20200513 3 1
11 20200513 4 1
12 20200514 1 50
13 20200514 2 44
14 20200514 3 2
15 20200514 4 4
16 20200515 1 48
17 20200515 2 44
18 20200515 3 6
19 20200515 4 5
20 20200516 1 47
21 20200516 2 23
22 20200516 3 0
23 20200516 4 1
24 20200517 1 48
25 20200517 2 14
26 20200517 3 1
27 20200517 4 0
So I want to roll, say 3 days at a time every 1 day and get the sum of cnt_alerts
per severity. This means that the first operation will be done for days (cdateint): [20200511, 20200512, 20200513] and the result should be:
- For severity 1: [48 + 48 + 47]
- For severity 2: [89 + 69 + 62]
- For severity 3: [5 + 1 + 1]
- For severity 4: [1 + 0 + 1]
The next operation will be for days (cdateint): [20200512, 20200513, 20200514]
- For severity 1: [48 + 47 + 50]
- For severity 2: [69 + 62 + 44]
- For severity 3: [1 + 1 + 2]
- For severity 4: [0 + 1 + 4]
And so on.
Note that for all days there will always be 4 severity levels and they are always sorted as you see on the example above.
UPDATE June 25 2020:
In addition I would like to get the cdateint
'labels'. i.e. for the first case, the label is 20200511 - 20200513
DATA
df = pd.DataFrame({'cdateint': {0: u'20200511', 1: u'20200511', 2: u'20200511', 3: u'20200511', 4: u'20200512', 5: u'20200512', 6: u'20200512', 7: u'20200512', 8: u'20200513', 9: u'20200513', 10: u'20200513', 11: u'20200513', 12: u'20200514', 13: u'20200514', 14: u'20200514', 15: u'20200514', 16: u'20200515', 17: u'20200515', 18: u'20200515', 19: u'20200515', 20: u'20200516', 21: u'20200516', 22: u'20200516', 23: u'20200516', 24: u'20200517', 25: u'20200517', 26: u'20200517', 27: u'20200517'},
'cnt_alerts': {0: 48, 1: 89, 2: 5, 3: 1, 4: 48, 5: 69, 6: 1, 7: 0, 8: 47, 9: 62, 10: 1, 11: 1, 12: 50, 13: 44, 14: 2, 15: 4, 16: 48, 17: 44, 18: 6, 19: 5, 20: 47, 21: 23, 22: 0, 23: 1, 24: 48, 25: 14, 26: 1, 27: 0},
'severity': {0: 1, 1: 2, 2: 3, 3: 4, 4: 1, 5: 2, 6: 3, 7: 4, 8: 1, 9: 2, 10: 3, 11: 4, 12: 1, 13: 2, 14: 3, 15: 4, 16: 1, 17: 2, 18: 3, 19: 4, 20: 1, 21: 2, 22: 3, 23: 4, 24: 1, 25: 2, 26: 3, 27: 4}})