I am working on a class project and have the code I pasted here. It created 3 dataframes of stock data, close prices, volumes and dividends. All the data has been pivoted so the dates are the index, columns are the tickers and values are the previous mentioned. The question asks to create an index of weights based on getting the percentage of cash in each stock (so price x volume at each date/sum(price x volume for all the dates of a particular ticker) (note: this is how we are told to calculate the weight based on the instructions). I wrote the code in the function but initially I set axis=0 because this would add up the values in each column (iteration down the rows). However, this answer was not accepted, and the correct answer is axis=1. This makes no sense to me as this would add up the prices for every stock on a given date rather than for all the dates of a given stock wouldn't it? Am I missing something?
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)