基于当前行和之后的 N 行的计算列

我正在处理 1000 多行股票数据,目前正在将其加载到数据框中(我也是使用 pandas 的新手,但目前它很棒,我仍在学习):


          open      high       low     close    volume        date

0     339.0500  339.6100  336.6200  337.2300  68054244  2020-08-19

1     338.3400  339.1000  336.6100  338.6400  38733908  2020-08-18

2     337.9400  338.3400  336.8517  337.9100  34496002  2020-08-17

3     336.4100  337.4200  335.6200  336.8400  47260390  2020-08-14

4     336.6100  338.2514  335.8300  336.8300  41816146  2020-08-13

...        ...       ...       ...       ...       ...         ...

5229  138.6250  139.1093  136.7812  137.8750   7431500  1999-11-05

5230  136.7500  137.3593  135.7656  136.5312   7907500  1999-11-04

5231  136.0000  136.3750  135.1250  135.5000   7222300  1999-11-03

5232  135.9687  137.2500  134.5937  134.5937   6516900  1999-11-02

5233  136.5000  137.0000  135.5625  135.5625   4006500  1999-11-01

我有一个简单移动平均线的动态平均值数组,我想根据列的动态选择对每一行执行该计算,例如,我选择了一个基于列的 5 天简单移动平均线open:


          open      high       low     close    volume        date       5-day avg

0      |339.0500|  339.6100  336.6200  337.2300  68054244  2020-08-19      |337.67|

1     >|338.3400|  339.1000  336.6100  338.6400  38733908  2020-08-18     >297.585

2     >|337.9400|  338.3400  336.8517  337.9100  34496002  2020-08-17       ...

3     >|336.4100|  337.4200  335.6200  336.8400  47260390  2020-08-14       ...

4     >|336.6100|  338.2514  335.8300  336.8300  41816146  2020-08-13       ...

5229  >138.6250  139.1093  136.7812  137.8750   7431500  1999-11-05         ...

我最接近的是:


for avg in avgs:

            overview[avg] = {}

            for i in range(avg):

                overview[avg][i] = {}

                overview[avg][i] = df.loc[df['date'] <= str((date - td(days=(avg - i)))), ['close']].head(avg).sum(numeric_only=True).div(avg).to_json()

但我觉得它有点笨拙,不是一种非常有效的方法。我看过做:


df['5-Day Avg'] = df['open'].head(5).sum().div(5) ....

但是不会做我想做的事情,因为我不断地从顶部而不是从当前索引中抓取。


HUH函数
浏览 83回答 2
2回答

温温酱

您可以对值进行排序并使用df.rolling(5)['open'].mean()df = df.sort_values('date')df['5-day avg'] = df.rolling(5)['open'].mean()df = df.sort_values('date', ascending=False)dfOut[184]:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; open&nbsp; &nbsp; &nbsp; high&nbsp; &nbsp; &nbsp; &nbsp;low&nbsp; &nbsp; &nbsp;close&nbsp; &nbsp; volume&nbsp; &nbsp; &nbsp; &nbsp; date&nbsp; 5-day avg0&nbsp; &nbsp; &nbsp;339.0500&nbsp; 339.6100&nbsp; 336.6200&nbsp; 337.2300&nbsp; 68054244&nbsp; 2020-08-19&nbsp; 337.670001&nbsp; &nbsp; &nbsp;338.3400&nbsp; 339.1000&nbsp; 336.6100&nbsp; 338.6400&nbsp; 38733908&nbsp; 2020-08-18&nbsp; 297.585002&nbsp; &nbsp; &nbsp;337.9400&nbsp; 338.3400&nbsp; 336.8517&nbsp; 337.9100&nbsp; 34496002&nbsp; 2020-08-17&nbsp; 257.267003&nbsp; &nbsp; &nbsp;336.4100&nbsp; 337.4200&nbsp; 335.6200&nbsp; 336.8400&nbsp; 47260390&nbsp; 2020-08-14&nbsp; 216.879004&nbsp; &nbsp; &nbsp;336.6100&nbsp; 338.2514&nbsp; 335.8300&nbsp; 336.8300&nbsp; 41816146&nbsp; 2020-08-13&nbsp; 176.790745229&nbsp; 138.6250&nbsp; 139.1093&nbsp; 136.7812&nbsp; 137.8750&nbsp; &nbsp;7431500&nbsp; 1999-11-05&nbsp; 136.768745230&nbsp; 136.7500&nbsp; 137.3593&nbsp; 135.7656&nbsp; 136.5312&nbsp; &nbsp;7907500&nbsp; 1999-11-04&nbsp; &nbsp; &nbsp; &nbsp; NaN5231&nbsp; 136.0000&nbsp; 136.3750&nbsp; 135.1250&nbsp; 135.5000&nbsp; &nbsp;7222300&nbsp; 1999-11-03&nbsp; &nbsp; &nbsp; &nbsp; NaN5232&nbsp; 135.9687&nbsp; 137.2500&nbsp; 134.5937&nbsp; 134.5937&nbsp; &nbsp;6516900&nbsp; 1999-11-02&nbsp; &nbsp; &nbsp; &nbsp; NaN5233&nbsp; 136.5000&nbsp; 137.0000&nbsp; 135.5625&nbsp; 135.5625&nbsp; &nbsp;4006500&nbsp; 1999-11-01&nbsp; &nbsp; &nbsp; &nbsp; NaN

慕的地8271018

在具有窗口大小的熊猫中尝试滚动方法来计算滚动平均值并移动结果。df['5-Day Avg'] = df['open'].rolling(5).mean().shift(periods=-4)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python