python-3.x - 如何使用groupby计算vwap(成交量加权平均价格)?

标签 python-3.x pandas machine-learning

我想计算每个月的 VWAP 值(即创建的每个组)。

data['Month'] = pd.DatetimeIndex(data['Date']).month
data.head()
data['Year'] = pd.DatetimeIndex(data['Date']).year
data.head()
group = data.groupby(['Month', 'Year'])
group.first() 
data['VWAP'] = (np.cumsum(data['Close Price' * data['Total Traded 
Quantity']) / np.cumsum(data['Total Traded Quantity']))

属性错误:“DataFrame”对象没有属性“数量”

最佳答案

你忘记在数据['Close Price']后面写']',这是我的代码

df['Month'] = pd.DatetimeIndex(df['Date']).month
df['Year'] = pd.DatetimeIndex(df['Date']).year
group = df.groupby(['Month', 'Year'])
df['VWAP'] = (np.cumsum(df['Close Price'] * df['Total Traded Quantity']) / np.cumsum(df['Total Traded Quantity']))

谢谢...

关于python-3.x - 如何使用groupby计算vwap(成交量加权平均价格)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56670850/

相关文章:

python - python中Random模块如何生成一个新数字

python - 将多个数组合并为日期时间

python - 将 Sum 添加到 pandas 数据框中的所有分组行

python - 为什么标签与 pandas、itertools 和 numpy 索引不一致?

machine-learning - Theano 支持 GPU 无法在 google Colab 上运行

image-processing - 如何识别图片中我们感兴趣的物体类型

python-3.x - PyTorch:用于训练和测试/验证的不同前向方法

python - python 中与本地和全局库的相对导入

django - Django 2.0 url()到path()

machine-learning - 使用tiny-dnn 训练 CNN 获取 NIST 数字