Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Data Science by (17.6k points)

I would like to add a cumulative sum column to my Pandas dataframe so that:

name | day       | no

-----|-----------|----

Jack | Monday    | 10

Jack | Tuesday   | 20

Jack | Tuesday   | 10

Jack | Wednesday | 50

Jill | Monday    | 40

Jill | Wednesday | 110

becomes:

Jack | Monday     | 10  | 10

Jack | Tuesday    | 30  | 40

Jack | Wednesday  | 50  | 90

Jill | Monday     | 40  | 40

Jill | Wednesday  | 110 | 150

I tried various combos of df.groupby and df.agg(lambda x: cumsum(x)) to no avail.

1 Answer

0 votes
by (41.4k points)

Use groupby() twice:

In [52]:

print df

   name        day   no

0  Jack     Monday   10

1  Jack    Tuesday   20

2  Jack    Tuesday   10

3  Jack  Wednesday   50

4  Jill     Monday   40

5  Jill  Wednesday  110

In [53]:

print df.groupby(by=['name','day']).sum().groupby(level=[0]).cumsum()

                 no

name day           

Jack Monday      10

     Tuesday     40

     Wednesday   90

Jill Monday      40

     Wednesday  150

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...