假设我想测量一个器官的长度,假设几个物种的胃,按类型排序,我从具有重复值的.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
您只需要更正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