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

时间:2019-06-19 15:06:45

标签: 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']))

AttributeError:“ DataFrame”对象没有属性“ quantity”

1 个答案:

答案 0 :(得分:1)

您忘了在数据['收盘价']之后写']',这是我的代码

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']))

谢谢...