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? 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?

Upvotes: 3

Views: 6255

Answers (1)

David Erickson
David Erickson

Reputation: 16683

Per the comments, you can groupby the index and return the cumcount() in a new object s. Then, you can groupby this new object s and get the sum(). I am assuming index is on your index in your example and not a column called index. If it is a column called index, then first do 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:

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

Upvotes: 5

Related Questions