简体   繁体   中英

Pandas cumulative diff from groupby

I need to calculate the difference from the beginning of a MultiIndex level, to calculate the decay from start of a level. My example input and output will look something like this:

               values
place time     
A     a           120
      b           100
      c            90
      d            50
B     e            11
      f            12
      g            10
      h             9

               values

A     a           NaN
      b           -20
      c           -30
      d           -70
B     e           Nan
      f            +1
      g            -1
      h            -2

I can use a grouby to get the diff between consecutive cells in a level:

df.groupby(level=0)['values'].diff()

But that's not quite what I want!

Alas, the accepted answer isn't quite what I want. I've got a better example:

arrays = [np.array(['bar', 'bar', 'bar', 'foo', 'foo', 'foo']),
          np.array(['one', 'two', 'three', 'one', 'two', 'three'])]
df = pd.DataFrame([1000, 800, 500, 800, 400, 200], index=arrays)

   bar one    1000
       two     800
       three   500
   foo one     800
       two     400
       three   200

    expected_result = pd.DataFrame([Nan, -200, -500, Nan, -400, -600], index=arrays)

   bar one      Nan
       two     -200
       three   -500
   foo one     Nan 
       two     -400
       three   -600

But the result of df.groupby(level=0).diff().cumsum() gives:

pd.DataFrame([Nan, -200, -500, Nan, -900, -1100], index=arrays)

   bar one      Nan
       two     -200
       three   -500
   foo one      Nan 
       two     -900
       three   -1100

你在寻找一个cumsum吗?

df.groupby(level=0)['values'].diff().cumsum()

You can get what I wanted by chaining another groupby :

arrays = [np.array(['bar', 'bar', 'bar', 'foo', 'foo', 'foo']),
      np.array(['one', 'two', 'three', 'one', 'two', 'three'])]
df = pd.DataFrame([1000, 800, 500, 800, 400, 200], index=arrays)

   bar one    1000
       two     800
       three   500
   foo one     800
       two     400
       three   200

    expected_result = pd.DataFrame([Nan, -200, -500, Nan, -400, -600], index=arrays)

df.groupby(level=0).diff().groupby(level=0).cumsum()

    bar one      Nan
       two     -200
       three   -500
    foo one     Nan 
       two     -400
       three   -600

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM