如何从之前的 window 中获得均值的滚动均值
how to get a rolling mean with mean from previous window
我正在拼命寻找 pandas 的解决方案。也许你能帮我。
我正在寻找一个考虑到先前均值的滚动均值。
df 看起来像这样:
index
count
0
4
1
6
2
10
3
12
现在,使用 rolling(window=2).mean()
函数我会得到这样的结果:
index
count
r_mean
0
4
NaN
1
6
5
2
10
8
3
12
11
我想考虑第一次计算的平均值,如下所示:
index
count
r_mean
0
4
NaN
1
6
5
2
10
7.5
3
12
9.5
其中,
row1: (4+6)/2=5
row2: (5+10)/2=7.5
row3: (7.5+12)/2=9.75
提前致谢!
我们可以为此使用简单的 python
循环,如果您想加快速度,可以尝试 numba
l= []
n = 2
for x,y in zip(df['count'],df.index):
try :
l.append(np.nansum(x+l[y-n+1])/n)
except:
l.append(x)
df.loc[n-1:, 'new']=l[n-1:]
df
Out[332]:
index count new
0 0 4 NaN
1 1 6 5.00
2 2 10 7.50
3 3 12 9.75
编辑: 实际上 pandas 中实现的方法 ewm
可以执行此计算
df['res'] = df['count'].ewm(alpha=0.5, adjust=False, min_periods=2).mean()
原回答:这里有一个方法。因为一切都可以以系数为2的幂来开发。
# first create a series with power of 2
coef = pd.Series(2**np.arange(len(df)), df.index).clip(lower=2)
df['res'] = (coef.div(2)*df['count']).cumsum()/coef
print(df)
index count res
0 0 4 2.00
1 1 6 5.00
2 2 10 7.50
3 3 12 9.75
如果需要,您可以用 df.loc[0, 'res'] = np.nan
屏蔽第一个值
我正在拼命寻找 pandas 的解决方案。也许你能帮我。
我正在寻找一个考虑到先前均值的滚动均值。
df 看起来像这样:
index | count |
---|---|
0 | 4 |
1 | 6 |
2 | 10 |
3 | 12 |
现在,使用 rolling(window=2).mean()
函数我会得到这样的结果:
index | count | r_mean |
---|---|---|
0 | 4 | NaN |
1 | 6 | 5 |
2 | 10 | 8 |
3 | 12 | 11 |
我想考虑第一次计算的平均值,如下所示:
index | count | r_mean |
---|---|---|
0 | 4 | NaN |
1 | 6 | 5 |
2 | 10 | 7.5 |
3 | 12 | 9.5 |
其中,
row1: (4+6)/2=5
row2: (5+10)/2=7.5
row3: (7.5+12)/2=9.75
提前致谢!
我们可以为此使用简单的 python
循环,如果您想加快速度,可以尝试 numba
l= []
n = 2
for x,y in zip(df['count'],df.index):
try :
l.append(np.nansum(x+l[y-n+1])/n)
except:
l.append(x)
df.loc[n-1:, 'new']=l[n-1:]
df
Out[332]:
index count new
0 0 4 NaN
1 1 6 5.00
2 2 10 7.50
3 3 12 9.75
编辑: 实际上 pandas 中实现的方法 ewm
可以执行此计算
df['res'] = df['count'].ewm(alpha=0.5, adjust=False, min_periods=2).mean()
原回答:这里有一个方法。因为一切都可以以系数为2的幂来开发。
# first create a series with power of 2
coef = pd.Series(2**np.arange(len(df)), df.index).clip(lower=2)
df['res'] = (coef.div(2)*df['count']).cumsum()/coef
print(df)
index count res
0 0 4 2.00
1 1 6 5.00
2 2 10 7.50
3 3 12 9.75
如果需要,您可以用 df.loc[0, 'res'] = np.nan
屏蔽第一个值