pandas multiindex ewma滚动每个样本类型

时间:2017-03-30 13:56:13

标签: python pandas

假设我想测量一个器官的长度,让我们说几个物种的胃,按类型排序, 我使用重复值从.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的语法是什么,保持每个物种分开(我不想滚动所有的措施,但是只在海豚或鲸鱼之一上)

我试过了

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

但它只会覆盖所有物种,而不是对它进行区分......

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

1 个答案:

答案 0 :(得分:0)

你需要更正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