dataframe math in pandas
<p><strong>TOTALLY RE WROTE ORIGINAL QUESTION</strong></p>

<p>I read raw data from a csv file "CloseWeight4.csv"</p>

<pre><code>df=pd.read_csv('CloseWeights4.csv')
Date      Symbol    ClosingPrice    Weight
3/1/2010    OGDC    116.51         0.1820219
3/2/2010    OGDC    117.32         0.1820219
3/3/2010    OGDC    116.4          0.1820219
3/4/2010    OGDC    116.58         0.1820219
3/5/2010    OGDC    117.61         0.1820219
3/1/2010    WTI      78.7          0.5348142
3/2/2010    WTI      79.68         0.5348142
3/3/2010    WTI      80.87         0.5348142
3/4/2010    WTI      80.21         0.5348142
3/5/2010    WTI      81.5          0.5348142
3/1/2010    FX       85.07         0.1312427
3/2/2010    FX       85.1077       0.1312427
3/3/2010    FX       85.049        0.1312427
3/4/2010    FX       84.9339       0.1312427
3/5/2010    FX       84.8          0.1312427
3/1/2010    PIB      98.1596499    0.1519211
3/2/2010    PIB      98.1596499    0.1519211
3/3/2010    PIB      98.1764222    0.1519211
3/4/2010    PIB      98.1770656    0.1519211
3/5/2010    PIB      98.1609364    0.1519211
</code></pre>

<p>From Which I generate a dataframe df2</p>

<pre><code>df2=df.iloc[:,0:3].pivot('Date', 'Symbol', 'ClosingPrice')

df2
Out[10]: 
Symbol           FX    OGDC        PIB    WTI
Date                                         
2010-03-01  85.0700  116.51  98.159650  78.70
2010-03-02  85.1077  117.32  98.159650  79.68
2010-03-03  85.0490  116.40  98.176422  80.87
2010-03-04  84.9339  116.58  98.177066  80.21
2010-03-05  84.8000  117.61  98.160936  81.50
</code></pre>

<p>from this I calculate returns using:</p>

<pre><code>ret=np.log(df2/df2.shift(1))

In [12] ret

Out[12]: 
Symbol            FX      OGDC       PIB       WTI
Date                                              
2010-03-01       NaN       NaN       NaN       NaN
2010-03-02  0.000443  0.006928  0.000000  0.012375
2010-03-03 -0.000690 -0.007873  0.000171  0.014824
2010-03-04 -0.001354  0.001545  0.000007 -0.008195
2010-03-05 -0.001578  0.008796 -0.000164  0.015955
</code></pre>

<p>I have weights of each security from df </p>

<pre><code>df3=df.iloc[:,[1,3]].drop_duplicates().reset_index(drop=True)

df3
Out[14]: 
          Weight
Symbol          
OGDC    0.182022
WTI     0.534814
FX      0.131243
PIB     0.151921
</code></pre>

<p>I am trying to get the following weighted return results for each day but don't know how to do the math in  pandas:</p>

<pre><code>Date        Portfolio_weighted_returns
2010-03-02        0.008174751
2010-03-03        0.006061657
2010-03-04       -0.005002414
2010-03-05        0.009058151
where the Portfolio_weighted_returns of 2010-03-02 is calculated as follows:
0.006928*0.182022+.012375*0.534814+0.000443*0.131243+0*0.151921 = 0.007937512315
</code></pre>

<p>I then need to have these results multiplied by a decay factor where the decay factor is defineD as decFac =decay^(t).  Using a decay = 0.5 gives decFac values of:</p>

<pre><code>Date        decFac
2010-03-02  0.0625
2010-03-03  0.125
2010-03-04  0.25
2010-03-05  0.5
</code></pre>

<p>I then need to take the SQRT of the sum of the squared Portfolio_weighted_returns for each day multiplied by the respective decFac as such:</p>

<pre><code>SQRT(Sum(0.008174751^2*.0625+0.006061657^2*.125+(-0.005002414^2)*.25+.009058151^2*.5)) = 0.007487
</code></pre>

<pre><code>import numpy as np
import pandas as pd
</code></pre>

<h2>define the variables</h2>

<pre><code>data = np.mat('''  85.0700  116.51  98.159650  78.70;
85.1077  117.32  98.159650  79.68;
85.0490  116.40  98.176422  80.87;
84.9339  116.58  98.177066  80.21;
84.8000  117.61  98.160936  81.50''')

cols = ['FX', 'OGDC' , 'PIB' , 'WTI']
dts = pd.Series( data=pd.date_range('2010-03-01', '2010-03-05'), name='Date' )
df2 = pd.DataFrame( data=data, columns=cols, index=dts )
# this is your df3 variable
wgt = pd.DataFrame( data=[0.131243, 0.182022, 0.151921, 0.534814], index=pd.Series(cols, name='Symbol') , columns=['Weight'] )
</code></pre>

<p>To calculate daily returns I use the <code>.shift</code> operator</p>

<pre><code># Calculate the daily returns for each security
df_ret = np.log( df2 / df2.shift(1) )
#                   FX      OGDC       PIB       WTI
# Date                                              
# 2010-03-01       NaN       NaN       NaN       NaN
# 2010-03-02  0.000443  0.006928  0.000000  0.012375
# 2010-03-03 -0.000690 -0.007873  0.000171  0.014824
# 2010-03-04 -0.001354  0.001545  0.000007 -0.008195
# 2010-03-05 -0.001578  0.008796 -0.000164  0.015955
</code></pre>

<p>You need to multiply the <code>Weight</code> column of <code>wgt</code> with <code>ret</code> to get the desired result. <code>wgt['Weight']</code> will return a <code>pd.Series</code> which is more like a 1-D array than a 2D array which a <code>pd.DataFrame</code> can be generally thought of.</p>

<pre><code>df_wgt_ret = wgt['Weight'] * df_ret
#                   FX      OGDC           PIB       WTI
# Date                                                  
# 2010-03-01       NaN       NaN           NaN       NaN
# 2010-03-02  0.000081  0.003705  0.000000e+00  0.001880
# 2010-03-03 -0.000126 -0.004210  2.242285e-05  0.002252
# 2010-03-04 -0.000247  0.000826  8.609014e-07 -0.001245
# 2010-03-05 -0.000287  0.004704 -2.156434e-05  0.002424
</code></pre>

<p>Sum over the columns (axis=1) to get the portfolio returns. Note this returns a <code>pd.Series</code> not a dataframe</p>

<pre><code>port_ret = df_wgt_ret.sum(axis=1)
# Date
# 2010-03-01         NaN
# 2010-03-02    0.005666
# 2010-03-03   -0.002061
# 2010-03-04   -0.000664
# 2010-03-05    0.006820
</code></pre>

<p>Finally, multiply the decay rate with the portfolio, note that because the operation happens over the columns you need to </p>

<pre><code>total_ret = (port_ret * sr_dec).sum()
final_res = total_ret**0.5
</code></pre>

<h2>The One liner</h2>

<p>I'm assuming <code>decFac</code> is a dataframe with column name <code>decFac</code> and using <code>df3</code> and <code>ret</code> as you've defined them.</p>

<pre><code>result = (( (df3.Weight * ret).sum(axis=1)**2 * decFac.decFac ).sum())**.5
</code></pre>

python pandas dataframe