Anastasia Anastasia - 6 months ago 19
Python Question

Pandas: sum all rows

I have a

DataFrame
that looks like this:

score num_participants
0 20
1 15
2 5
3 10
4 12
5 15


I need to find the number of participants with
score
that is greater than or equal to the
score
in the current row:

score num_participants num_participants_with_score_greater_or_equal
0 20 77
1 15 57
2 5 42
3 10 37
4 12 27
5 15 15


So, I am trying to sum current row and all rows below it. The data has around 5000 rows, so I can't manually set it by indexing.
cumsum
doesn't do the trick and I am not sure if there is a simple way to do this. I have spent quite some time trying to solve this, so any help would be appreciated.

Answer

This is a reverse cumsum. Reverse the list, cumsum, then reverse back.

df.ix[::-1].cumsum().ix[::-1]

   score  num_participants
0     15                77
1     15                57
2     14                42
3     12                37
4      9                27
5      5                15
Comments