Are you really sure that you want aggregation over week days? That loses the index, and also the cumulative sum makes less sense if there are multiple weeks. The answers by dmitry-andreev and @vjayky calculates cumsum over the sequence of days for each name instead. Think of how this could be extended if there were a date column too, that the entries could be sorted by before grouping and aggregating.
– Elias HasleNov 8 at 8:09

Thanks for the answer. I did have some queries though: 1. Can you please explain what does 'level = [0]' mean? 2. Also, as you can see, you had row numbers in your data frame before and these row numbers go away once you do the cumulative sum. Is there a way to have them back?
– user3694373Oct 16 '14 at 21:31

2

1), The index number has to go, as the cumsums are from multiple rows, like the 2nd number, 40, is 10+20+10, which index value should it get? 1, 2 or 3? So, let's keep using name and day as multiIndex, which make better sense (reset_index() to get int index, if desired). 2), the level=[0] means groupby is to operate by the 1st level of MultiIndex, namely column name.
– CT ZhuOct 17 '14 at 0:23

Thanks CT. I understood that later and tried reset_index() to solve my issue. Thanks for the detailed explanation!
– user3694373Oct 17 '14 at 1:59

1

There's a subtle bug: the first groupby() defaults to sorting the keys, so if you add a Jack-Thursday row at the bottom of the input dataset you'll get unexpected results. And since groupby() can work with level names I find df.groupby(['name', 'day'], sort=False).sum().groupby(by='name').cumsum().reset_index() less cryptic.
– NickolayMay 27 at 22:57

Showing how to add it back to the df is really helpful. I tried using a transform, but that didn't play nicely with cumsum().
– zerovectorMay 26 '16 at 10:09

Note that this answer (seems equivalent to the simpler solution by @vjayky) does not aggregate by name and day before calculating the cumulative sum by name (note: there are 2 rows for Jack+Tuesday in the result). This is what makes it simpler than the answer by CT Zhu.
– NickolayMay 27 at 22:09

Thanks for posting this, it helped me understand what's going on here! Note that groupby().sum() is not just moving both columns to MultiIndex -- it also sums up the two values for Jack+Tuesday. And as_index=False doesn't seem to have any effect in this case, since the index was already set before the groupby. And since groupby().cumsum() nukes the name/day from the data frame's columns, you have to either add the resulting numeric column to the original data frame (like vjayky and Dmitry suggested), or move name/day to index, and reset_index afterwards.
– NickolayMay 27 at 22:39

This calculates a global running total, instead of a separate sum for each group separately. So Jill-Monday gets assigned a value of 130 (90, as the sum of all Jack's values, + 40, the value for Jill-Monday).
– NickolayMay 27 at 22:46

@Nickolay just added another answer let me know if it works
– sushmitMay 30 at 17:42