[英]Python pandas rolling mean without the window num fixed
I am looking to add two column [ std_dev , mean ], where the sample of the mean expands as the date continues for the specific location. 我想添加两列[ std_dev , mean ],其中均值的样本随着特定位置的日期继续而扩展。
location date temp std_dev mean
NY 2014-02-01 60
NY 2014-02-02 55
NY 2014-02-03 70
NY 2014-02-04 80
LA 2014-02-01 80
LA 2014-02-02 85
LA 2014-02-03 75
I found a post that explains rolling mean/std and I was able to apply it to the table. 我发现了一个解释滚动平均值/标准的帖子,我能够将它应用到表格中。 However I receive an error for std_dev since the size of the location is not a fixed value.
但是我收到std_dev的错误,因为该位置的大小不是固定值。 How do I reference the window size without it being fixed?
如何在不修复窗口大小的情况下参考窗口大小?
pandas rolling on a shifted dataframe 大熊猫在移位的数据帧上滚动
df['mean'] = df.groupby('location')['temp'].apply(pd.rolling_mean,4,min_periods=2).shift(1)
df['std_dev'] = df.groupby('location')['temp'].apply(pd.rolling_std,4,min_periods=2).shift(1)
Any help greatly appreciated! 任何帮助非常感谢!
I think you are looking for expanding
, eg 我认为你正在寻求
expanding
,例如
>>> df
temp location
0 60 NY
1 55 NY
2 70 NY
3 80 NY
4 80 LA
5 85 LA
6 75 LA
>>> expander = df.groupby('location').temp.expanding(min_periods=2)
>>> orderify = lambda x: x.reset_index(level=0, drop=True).sort_index()
>>> df['mean'], df['std'] = map(orderify, [expander.mean(), expander.std()])
>>> df
location temp mean std
0 NY 60 NaN NaN
1 NY 55 57.500000 3.535534
2 NY 70 61.666667 7.637626
3 NY 80 66.250000 11.086779
4 LA 80 NaN NaN
5 LA 85 82.500000 3.535534
6 LA 75 80.000000 5.000000
Note : It would be nice to use .agg
here on expander
, but as of version 0.19.2 there isn't sophisticated agg
available on groupby.rolling
or groupby.expanding
, so it is not possible. 注意 :在
expander
上使用.agg
会很好,但是从版本0.19.2开始,在groupby.rolling
或groupby.expanding
上没有复杂的agg
,所以这是不可能的。 See 看到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.