Reputation: 49
I grouped a data frame by week number and get a column of numbers that looks like this
0 0.0
1 0.0
2 0.0
3 0.0
4 0.0
5 0.0
6 0.0
7 0.0
8 0.0
9 0.0
10 0.0
11 0.0
12 0.0
13 0.0
14 0.0
15 0.0
16 235.0
17 849.0
18 1013.0
19 1155.0
20 1170.0
21 1247.0
22 1037.0
23 1197.0
24 1125.0
25 1106.0
26 1229.0
I used the following line of code on the column: df_group['rolling_total'] = df_group['totals'].rolling(2).sum()
This is my desired result:
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
235.0
1084.0
2097.0
3252.0
4422.0
5669.0
6706.0
7903.0
9028.0
10134.0
11363.0
I get this instead:
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
235.0
1084.0
1862.0
2168.0
2325.0
2417.0
2284.0
2234.0
2322.0
2231.0
2335.0
All I want is the rolling sum of the column. Is .rolling() not the way to accomplish this? Is there something I am doing wrong?
Upvotes: 2
Views: 49
Reputation: 195408
Use .cumsum()
, not .rolling()
:
print(df.cumsum())
Prints:
column1
0 0.0
1 0.0
2 0.0
3 0.0
4 0.0
5 0.0
6 0.0
7 0.0
8 0.0
9 0.0
10 0.0
11 0.0
12 0.0
13 0.0
14 0.0
15 0.0
16 235.0
17 1084.0
18 2097.0
19 3252.0
20 4422.0
21 5669.0
22 6706.0
23 7903.0
24 9028.0
25 10134.0
26 11363.0
Upvotes: 2