我正在做一个类项目,并有我粘贴在这里的代码。它创建了股票数据、收盘价、成交量和股息的3个数据帧。所有的数据都经过数据透视,所以日期是索引,列是标记符,值是前面提到的。这个问题要求根据每个股票中现金的百分比创建一个权重指数(因此,每个日期的价格x成交量/总和(特定股票的所有日期的价格x成交量)(注:这是我们被告知如何根据说明计算权重)。我在函数中编写了代码,但最初我将axis设置为0,因为这将增加每列中的值(向下迭代行)。但是,这个答案没有被接受,正确答案是axis=1。这对我来说毫无意义,因为这会把某一天每一只股票的价格加起来,而不是把某一只股票的所有日期的价格加起来,不是吗?我错过什么了吗?你知道吗
df = pd.read_csv('../../data/project_3/eod-quotemedia.csv')
percent_top_dollar = 0.2
high_volume_symbols = project_helper.large_dollar_volume_stocks(df, 'adj_close', 'adj_volume', percent_top_dollar)
df = df[df['ticker'].isin(high_volume_symbols)]
close = df.reset_index().pivot(index='date', columns='ticker', values='adj_close')
volume = df.reset_index().pivot(index='date', columns='ticker', values='adj_volume')
dividends = df.reset_index().pivot(index='date', columns='ticker', values='dividends')
def generate_dollar_volume_weights(close, volume):
"""
Generate dollar volume weights.
Parameters
----------
close : DataFrame
Close price for each ticker and date
volume : str
Volume for each ticker and date
Returns
-------
dollar_volume_weights : DataFrame
The dollar volume weights for each ticker and date
"""
assert close.index.equals(volume.index)
assert close.columns.equals(volume.columns)
#TODO: Implement function
close_adj = close * volume
return close_adj.apply(lambda x: x/x.sum(), axis=1)
解释如下:
axis=1
逐行执行操作。axis=0
逐列执行操作。相关问题 更多 >
编程相关推荐