简体   繁体   English

按 Pandas 中的索引分组

[英]Groupby by index in Pandas

How can I use groupby by indexes (1,2,3)(they all are in the same order) and get the sum of the column score belonging to the range of each indexes?如何按索引(1,2,3)使用groupby(它们的顺序相同)并获得属于每个索引范围的列分数的总和? Basically I have this:基本上我有这个:

    index  score
    1      2
    2      2
    3      2
    1      3
    2      3
    3      3

What I want:我想要的是:

    index  score  sum
    1      2      6
    2      2      9
    3      2
    1      3
    2      3
    3      3

I understand it has to be something like this:我知道它必须是这样的:

    df = df.groupby(['Year'])['Score'].sum()

but instead of a Year, to somehow do it by indexes?但不是一年,而是通过索引以某种方式做到这一点?

Per the comments, you can groupby the index and return the cumcount() in a new object s .根据评论,您可以按索引分组并在新的groupby s中返回cumcount() Then, you can groupby this new object s and get the sum() .然后,您可以按这个新s分组并得到sum() I am assuming index is on your index in your example and not a column called index .我假设index在您的示例中的index上,而不是名为index的列。 If it is a column called index , then first do df = df.set_index('index') :如果它是一个名为index的列,那么首先执行df = df.set_index('index')

s = df.groupby(level=0).cumcount()
df.groupby(s)['score'].sum()

0    6
1    9
Name: score, dtype: int64

If you print out s , then s looks like this:如果你打印出s ,那么s看起来像这样:

    index
1    0
2    0
3    0
1    1
2    1
3    1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM