6

I have a data frame like the following (specific data below, this is generic). The no gives me a cumulative sum:

                 no
name day           
Jack Monday      10
     Tuesday     40
     Wednesday   90
Jill Monday      40
     Wednesday  150

I want to "unroll" the cumulative sum to give me something like this:

print df
   name        day   no
0  Jack     Monday   10
1  Jack    Tuesday   30
2  Jack  Wednesday   50
3  Jill     Monday   40
4  Jill  Wednesday  110

In essence, I'd like to do something like the following, but in reverse: Pandas groupby cumulative sum

Community
  • 1
  • 1
Afflatus
  • 2,302
  • 5
  • 25
  • 40
  • How many indices are there here – EdChum Apr 06 '16 at 19:27
  • I'm not sure what changed, but I cleaned up my file and reran your code and now it worked. I removed mention to any errors for clarity of the question. Thanks for your help. – Afflatus Apr 06 '16 at 19:35

2 Answers2

9

If I understand correctly you can do the following:

In [103]:
df.groupby(level=0).diff().fillna(df).reset_index()

Out[103]:
   name        day     no
0  Jack     Monday   10.0
1  Jack    Tuesday   30.0
2  Jack  Wednesday   50.0
3  Jill     Monday   40.0
4  Jill  Wednesday  110.0

So groupby the first index level and call diff to calculate the inter row differences per group and fill the NaN values with the original df values and call reset_index

tommy.carstensen
  • 8,962
  • 15
  • 65
  • 108
EdChum
  • 376,765
  • 198
  • 813
  • 562
0

Here's a method based on zip. It creates two series, the 2nd being offset by 1, and subtracts the difference between the two.

[n-nn for n,nn in zip(df['No'],df['No'][1:]+[0])]
BSalita
  • 8,420
  • 10
  • 51
  • 68