Python - Pandas - Unroll / Remove Cumulative Sum -


i have data frame following (specific data below, generic). no gives me cumulative sum:

                 no name day            jack monday      10      tuesday     40      wednesday   90 jill monday      40      wednesday  150 

i want "unroll" cumulative sum give me 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 following, in reverse: pandas groupby cumulative sum

iiuc can 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 first index level , call diff calculate inter row differences per group , fill nan values original df values , call reset_index


Comments