Pandas多指数ewma滚动每个样本类型

2024-05-15 16:34:45 发布

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

假设我想测量一个器官的长度,比如说几种动物的胃,按类型排列, 我从一个带有重复值的.csv创建了一个多索引数据帧,并且我每天都采集一个样本,使我的度量值变得嘈杂。在

我如何在最后60个样本上对多索引数据帧中包含的每个物种应用滚动ewma?在

数据帧示例:

arrays = [['mamal', 'mamal','mamal', 'mamal', 'mamal', 'mamal', 'mamal','mamal', 'mamal', 'mamal','bird', 'bird','bird', 'bird', 'reptile', 'reptile'],
          ['whale','whale','whale','whale', 'dolphin', 'dolphin', 'dolphin', 'dolphin', 'cat', 'cat', 'canary', 'canary', 'eagle', 'eagle', 'boa', 'turtle'],
          ['2017-03-01','2017-03-02','2017-03-03','2017-03-04','2017-03-01','2017-03-02','2017-03-03','2017-03-04','2017-03-03','2017-03-04','2017-03-01','2017-03-02','2017-03-03','2017-03-01','2017-03-02','2017-03-03','2017-03-01','2017-03-02','2017-03-03']]

tuples = list(zip(*arrays))

index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])

s = pd.Series(np.random.randn(13), index=index)
print(s) :

type     species  measure_date
mamal    whale    2017-03-01      0.913916
                  2017-03-02      0.860045
                  2017-03-03      1.166217
                  2017-03-04     -0.439948
         dolphin  2017-03-01      0.590208
                  2017-03-02      0.297475
                  2017-03-03      0.067966
                  2017-03-04     -0.477495
         cat      2017-03-03     -1.261023
                  2017-03-04     -0.931671
bird     canary   2017-03-01     -1.367815
                  2017-03-02     -0.820792
         eagle    2017-03-03     -0.532935
                  2017-03-01     -0.152090
reptile  boa      2017-03-02     -2.070819
         turtle   2017-03-03      1.329004
dtype: float64

假设我现在有更长的测量历史,保持一天一天的测量,对每个物种执行滚动ewma的语法是什么,保持每个物种是分开的(我不想覆盖所有的测量,只想对海豚或鲸鱼中的一个)

我试过了

^{pr2}$

但它只覆盖了所有的物种,并没有对它进行区分。。。在

type     species  measure_date
mamal    whale    2017-03-01           NaN
                  2017-03-02           NaN
                  2017-03-03           NaN
                  2017-03-04           NaN
         dolphin  2017-03-01      0.599637
                  2017-03-02      0.313861
                  2017-03-03      0.099954
                  2017-03-04     -0.476401
         cat      2017-03-03     -1.220229
                  2017-03-04     -0.918025
bird     canary   2017-03-01     -1.308843
                  2017-03-02     -0.786782
         eagle    2017-03-03     -0.553554
                  2017-03-01     -0.186791
reptile  boa      2017-03-02     -2.032299
         turtle   2017-03-03      1.272527

Tags: 数据index物种nancateaglecanaryturtle
1条回答
网友
1楼 · 发布于 2024-05-15 16:34:45

你只需要纠正level,正如@ScottBoston指出的:

s.groupby(level="second").apply(lambda x: pd.ewma(x,ignore_na=True,min_periods=2,adjust=True,com=0.030927835051546))

first    second   third     
mamal    whale    2017-03-01         NaN
                  2017-03-02    0.661551
                  2017-03-03   -0.726871
                  2017-03-04   -1.873301
         dolphin  2017-03-01         NaN
                  2017-03-02    0.242347
                  2017-03-03    0.276082
                  2017-03-04    0.071822
         cat      2017-03-03         NaN
                  2017-03-04    0.441826
bird     canary   2017-03-01         NaN
                  2017-03-02    1.426628
         eagle    2017-03-03         NaN
                  2017-03-01    0.382538
reptile  boa      2017-03-02         NaN
         turtle   2017-03-03         NaN
dtype: float64

相关问题 更多 >