[英]rolling mean in Pandas with fixed time window (instead of fixed nb. of observations)
我有一个具有两列和3级索引结构的数据框。 列是价格和数量,索引是交易者-库存-天。
我想为我的数据中的每个交易者-股票组合计算过去50天的价格和交易量的滚动平均值。
到目前为止,这是我想出的。
test = test.set_index(['date','trader','stock'])
test = test.unstack()。unstack()
test = test.resample(“ 1D”)
test = test.fillna(0)
test [[test.columns中col的col +'_ norm']] = test.apply(lambda x:pd.rolling_mean(x,50,50))
test.stack()。stack()。reset_index()。set_index(['trader','stock','date'])。sort_index()。head()
就是说,我两次解开数据集,以便只剩下时间轴,而且我可以计算变量的50天滚动平均值,因为50次观察对应于50天(重新采样数据后)。
问题是我不知道如何为滚动均值变量创建正确的名称
test [[test.columns中col的col +'_ norm']]
TypeError:只能将元组(不是“ str”)连接到元组
有什么主意在这里吗? 我的算法实际上是否正确才能获得这些滚动平均值? 非常感谢!
可以将pd.rolling_mean
(具有修改的列名)的结果与原始DataFrame连接起来:
means = pd.rolling_mean(test, 50, 50)
means.columns = [('{}_norm'.format(col[0]),)+col[1:] for col in means.columns]
test = pd.concat([test, means], axis=1)
import numpy as np
import pandas as pd
N = 10
test = pd.DataFrame(np.random.randint(4, size=(N, 3)),
columns=['trader', 'stock', 'foo'],
index=pd.date_range('2000-1-1', periods=N))
test.index.names = ['date']
test = test.set_index(['trader', 'stock'], append=True)
test = test.unstack().unstack()
test = test.resample("1D")
test = test.fillna(0)
means = pd.rolling_mean(test, 50, 50)
means.columns = [('{}_norm'.format(col[0]),)+col[1:] for col in means.columns]
test = pd.concat([test, means], axis=1)
test = test.stack().stack()
test = test.reorder_levels(['trader', 'stock', 'date'])
test = test.sort_index()
print(test.head())
产量
foo foo_norm
trader stock date
0 0 2000-01-01 0 NaN
2000-01-02 0 NaN
2000-01-03 0 NaN
2000-01-04 0 NaN
2000-01-05 0 NaN
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.