Search code examples
pythonpandasnumpysum

Pandas dataframe sum of row won't let me use result in equation


Anybody wish to help me understand why below code doesn't work?

start_date = '1990-01-01'
ticker_list = ['SPY', 'QQQ', 'IWM','GLD']
tickers = yf.download(ticker_list, start=start_date)['Close'].dropna()


ticker_vol_share = (tickers.pct_change().rolling(20).std()) \
                    / ((tickers.pct_change().rolling(20).std()).sum(axis=1))

Both (tickers.pct_change().rolling(20).std()) and ((tickers.pct_change().rolling(20).std()).sum(axis=1)) runs fine by themselves, but when ran together they form a dataframe with thousands of columns all filled with nan


Solution

  • Try this.

    rolling_std = tickers.pct_change().rolling(20).std()
    ticker_vol_share = rolling_std.apply(lambda row:row/sum(row),axis = 1)
    

    You will get enter image description here