Subtracting many columns in a df by one column in another df

Viewed 15521

I'm trying to substract a df "stock_returns" (144 rows x 517 col) by a df "p_df" (144 rows x 1 col).

I have tried;

stock_returns - p_df

stock_returns.rsub(p_df,axis=1)
stock_returns.substract(p_df)

But none of them work and all return Nan values.

I'm passing it through this fnc, and using the for loop to get args:

def disp_calc(returns, p, wi):    #apply(disp_calc, rows = ...)
    wi = wi/np.sum(wi)
    rp = (col_len(returns)*(returns-p)**2).sum()      #returns - p causing problems    
    return np.sqrt(rp)

for i in sectors:
    stock_returns = returns_rolling[sectordict[i]]#.apply(np.mean,axis=1)          
    portfolio_return = returns_rolling[i]; p_df = portfolio_return.to_frame()
    disp_df[i] = stock_returns.apply(disp_calc,args=(portfolio_return,wi))

My expected output is to subtract all 517 cols in the first df by the 1 col in p_df. so final results would still have 517 cols. Thanks

2 Answers
data['new_col3'] = data['col1'] - data['col2']
Related