Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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

like image 242
thomas.mac Avatar asked Jul 28 '17 22:07

thomas.mac


Video Answer


1 Answers

You're almost there, just need to set axis=0 to subtract along the indexes:

>>> stock_returns = pd.DataFrame([[10,100,200], 
                             [15, 115, 215], 
                             [20,120, 220], 
                             [25,125,225], 
                             [30,130,230]], columns=['A', 'B', 'C']) 
>>> stock_returns

   A    B    C
0  10  100  200
1  15  115  215
2  20  120  220
3  25  125  225
4  30  130  230

>>> p_df = pd.DataFrame([1,2,3,4,5], columns=['P'])
>>> p_df

   P
0  1
1  2
2  3
3  4
4  5

>>> stock_returns.sub(p_df['P'], axis=0)

    A    B    C
0   9   99  199
1  13  113  213
2  17  117  217
3  21  121  221
4  25  125  225
like image 69
kev8484 Avatar answered Sep 18 '22 14:09

kev8484