如何在滚动的基础上计算第10个收盘价的斜率参数,并将其加到表的最后一栏(收盘后,类似于移动平均价格)?

发布于 2022-03-09 15:09:10
基于Python Dataframe Find n rows rolling slope without for loop
假数据:
df = pd.DataFrame([[38611.38, '2022-03-08 22:23:00.000000'],
[38604.02, '2022-03-08 22:24:00.000000'],
[38611.76, '2022-03-08 22:25:00.000000'],
[38609.75, '2022-03-08 22:26:00.000000'],
[38601.35, '2022-03-08 22:27:00.000000']], columns = ['Close', 'Open time'])
df
Close Open time
0 38611.38 2022-03-08 22:23:00.000000
1 38604.02 2022-03-08 22:24:00.000000
2 38611.76 2022-03-08 22:25:00.000000
3 38609.75 2022-03-08 22:26:00.000000
4 38601.35 2022-03-08 22:27:00.000000
df.reset_index(drop=True)
window = 10
df['Close'].rolling(window).apply(lambda x: stats.linregress(x, x.index+1)[0], raw=False)结果:
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
...
995 0.017796
996 0.038905
997 0.052710
998 0.047330
999 0.043615
Name: Close, Length: 1000, dtype: float64https://stackoverflow.com/questions/70016504
复制相似问题