Pandas:求所有行的总和

3

我有一个类似于这样的DataFrame

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

我需要找到参与人数中分数大于或等于当前行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

所以,我想要对当前行及其以下所有行进行求和。数据大约有5000行,所以我无法通过索引手动设置它。cumsum不能解决这个问题,我也不确定是否有简单的方法可以实现。我已经花了相当多的时间尝试解决这个问题,所以希望能得到任何帮助。

2个回答

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

1
现在,由于“ix”的难以解释的语义,位置索引更倾向于使用“iloc”。 - DSM

1

除非score已经排序,否则怎么样?

df['num_participants_with_score_greater_or_equal'] = df.sort_values('score', ascending=False).num_participants.cumsum()

为了使score按正确顺序排列,您可以在之后使用.sort_index()来恢复原始顺序。


误解了您关于“需要找到分数较低的参与者”的观点。 - Stefan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接