How do I sum strings `n` at a time in pandas?
Given a data frame
A
0 14
1 59
2 38
3 40
4 99
5 89
6 70
7 64
8 84
9 40
10 30
11 94
12 65
13 29
14 48
15 26
16 80
17 79
18 74
19 69
This data frame contains 20 columns. I would like to group rows n=5
at a time and sum them up. So my output will look like this:
A
0 250
1 347
2 266
3 328
df.rolling_sum
won't help because it doesn't allow you to change the move when adding.
What are some other ways to do this?
df.set_index(df.index // 5).sum(level=0)
Assuming your indices are contiguous, you can perform integer division on df.index
and then group by index.
For the above, df
you can do this:
df.index // 5
# Int64Index([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3], dtype='int64')
To get a definitive answer is another step using and : df.groupby
dfGroupBy.sum
df.groupby(df.index // 5).sum()
A
0 250
1 347
2 266
3 328
If you don't RangeIndex
, use first and then group. df.reset_index
If you can manage the ndarray by the sum and not the series (you can always build the series again), you can use np.add.reduceat
.
np.add.reduceat(df.A.values, np.arange(0, df.A.size, 5))
Which in this case returns
array([250, 347, 266, 328])