Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用groupby计算vwap(成交量加权平均价格)?_Python 3.x_Pandas_Machine Learning - Fatal编程技术网

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

Python 3.x 如何使用groupby计算vwap(成交量加权平均价格)?,python-3.x,pandas,machine-learning,Python 3.x,Pandas,Machine Learning,我想计算每个月的VWAP值(即创建的每个组) AttributeError:'DataFrame'对象没有属性'quantity'如果您忘记在数据['Close Price']之后写入']',这是我的代码 df['Month'] = pd.DatetimeIndex(df['Date']).month df['Year'] = pd.DatetimeIndex(df['Date']).year group = df.groupby(['Month', 'Year']) df['VWAP'] =

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


AttributeError:'DataFrame'对象没有属性'quantity'

如果您忘记在数据['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']))

谢谢…

您可以添加一些示例数据吗?大多数情况下,5-10行就足以理解您的问题。您的代码不包含错误中所示的任何
数量。看起来您在Total Trade之后添加了换行符。这就是为什么它抱怨没有名为'Quantity'的属性。谢谢大家的帮助,我发现了这个错误。刚刚添加了此行“data['VWAP']=(data['Close Price']*data['Total trade Quantity'])。cumsum()/data['Total trade Quantity']。fillna(0)。cumsum()data.head()
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']))