来自groupby的Pandas累计差异

2024-09-24 04:23:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要计算从一个多指数能级开始的差,来计算从一个能级开始的衰变。我的示例输入和输出如下所示:

               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

我可以使用grouby来获得一个级别中连续单元格之间的差异:

^{pr2}$

但那不是我想要的!在

唉,公认的答案不是我想要的。我有一个更好的例子:

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

但是df.groupby(level=0).diff().cumsum()的结果给出:

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

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

Tags: dataframedfindexfoonpbarnanarray
2条回答

你在找cumsum之后吗?在

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

你可以通过链接另一个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

相关问题 更多 >