我有一个数据帧,看起来像:
a b
1 0.9 0.796522123
2 0.8 0.701075019
3 0.6 0.777130253
4 0.5 0.209912906
5 0.75 0.920537662
6 1 0.955212665
7 3.5 0.227221963
8 2 0.336632891
9 1.25 0.563511758
10 1 0.832624112
我想创建一个最大周期为3的移动平均值,其中每个观察值是df['a']*df['b
]。在
如果df['a'].rolling(window=3).sum() <= 3
,则MA为:
df['MA'] = (df['a']*df['b']).rolling(window=3).mean()
。在
但是,例如,如果df['a'].rolling(window=3).sum() > 3
,就像df[8:10]
的情况一样,那么我希望移动平均线是:
((1*0.83)+(1.25*0.832624112)+(0.75*0.336632891))/3
。在
我一直在尝试创建一个函数,然后应用它,比如:
^{pr2}$我无法实现这一点,我假设有一种更简单的方法使用pandas
或{
提前致谢。在
相关问题 更多 >
编程相关推荐