熊猫:总结所有行

Ana*_*sia 3 python dataframe pandas

我有一个DataFrame看起来像这样:

score num_participants
0     20
1     15
2     5
3     10
4     12
5     15 
Run Code Online (Sandbox Code Playgroud)

我需要找到score大于或等于score当前行的参与者数量:

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
Run Code Online (Sandbox Code Playgroud)

所以,我试图将当前行和它下面的所有行相加.数据有大约5000行,所以我无法通过索引手动设置它.cumsum我没有做到这一点,我不确定是否有一个简单的方法来做到这一点.我花了很多时间试图解决这个问题,所以任何帮助都会受到赞赏.

piR*_*red 8

这是相反的cumsum.反转列表,cumsum然后反向返回.

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

   score  num_participants
0     15                77
1     15                57
2     14                42
3     12                37
4      9                27
5      5                15
Run Code Online (Sandbox Code Playgroud)