0

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)
Nore Patel
  • 35
  • 7

1 Answers1

1

Here is the explanation:

  • axis=1 does the operations row by row.

  • axis=0 does the operations column by column.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • I understand that much. What I don't get is we are asked to divide each value in a dataframe by the sum of all the values in the row of that particular column. So if all there were for instance, 5 rows and each of the 5 values in the first column were 1 (so a column of five 1s), then we would want to return a dataframe with each value being 1/(1+1+1+1+1) or 1/5 for each value of that column. So wouldn't axis=0 in the following code: `df.apply(lambda x: x/x.sum(), axis=1)` But the lesson says axis=1 is correct – Nore Patel Jul 22 '19 at 07:58