Apply weight formula over rows of Dataframe Pandas
<p>I have a <code>df1</code> below. I make a copy of it to <code>df2</code> to conserve <code>df1</code>; then I use <code>df3</code> to compute over <code>df2</code>.</p>

<pre><code>df2=df1.copy()
</code></pre>

<p>I want to calculate a weight such as <code>Weight(A) = Price(A) / Sum(row_Prices)</code> and return it to <code>df2</code> below the prices such as for each row I get 3 lines of data, the price, the std and the weight row. I also want to calculate the std over the row and I suppose it is of a similar form.</p>

<p>I have tried this</p>

<pre><code>df3 = df2.iloc[1:,1:].div(df2.iloc[1:,1:].sum(axis=1), axis=0)
</code></pre>

<p>to get the weights and then print <code>df3</code> but it does not work.</p>

<p>For getting 2 rows for each date I tried stacking <code>.stack()</code> but I am probably doing it wrong. Help! Thank you</p>

<pre class="lang-none prettyprint-override"><code>                       A      B      C        D     E
2006-04-27 00:00:00                                    
2006-04-28 00:00:00  69.62  69.62  6.518   65.09  69.62
2006-05-01 00:00:00   71.5   71.5  6.522   65.16   71.5
2006-05-02 00:00:00  72.34  72.34  6.669   66.55  72.34
2006-05-03 00:00:00  70.22  70.22  6.662   66.46  70.22
2006-05-04 00:00:00  68.32  68.32  6.758   67.48  68.32
2006-05-05 00:00:00     68     68  6.805   67.99     68
2006-05-08 00:00:00  67.88  67.88  6.768   67.56  67.88
</code></pre>

<p>I would like it to ouput nicely as such:</p>

<pre class="lang-none prettyprint-override"><code>                            A      B      C        D     E
2006-04-27 00:00:00

2006-04-28 00:00:00                                    
            price        69.62  69.62  6.518   65.09  69.62
            weight
            std
2006-05-01 00:00:00  
            price         71.5   71.5  6.522   65.16   71.5
            weight
            std
2006-05-02 00:00:00   
            price        72.34  72.34  6.669   66.55  72.34
            weight
            std
</code></pre>

<p>As far as I know, there's no one-liner-quick-and-dirty way to achieve what you are trying to do.
You need to calculate all your data and then merge it all into a <code>DataFrame</code> that uses a multi-level index:</p>

<pre><code># Making weight/std DataFrames
cols = list('ABCDE')
weight = pd.DataFrame([df[col] / df.sum(axis=1) for col in df], index=cols).T
std = pd.DataFrame([df.std(axis=1) for col in df], index=cols).T

# Making MultiIndex DataFrame
mindex = pd.MultiIndex.from_product([['price', 'weight', 'std'], df.index])
new_df = pd.DataFrame(index=mindex, columns=cols)

# Inserting data
new_df.ix['price'] = df.values
new_df.ix['weight'] = weight.values
new_df.ix['std'] = std.values

# Swapping levels
new_df = new_df.swaplevel(0, 1).sort_index()
</code></pre>

<p>The resulting <code>new_df</code> should look somewhat like this:</p>

<pre><code>2006-04-28 price      69.62     69.62      6.518     65.09     69.62
           std      27.7829   27.7829    27.7829   27.7829   27.7829
           weight  0.248228  0.248228  0.0232397  0.232076  0.248228
2006-05-01 price       71.5      71.5      6.522     65.16      71.5
           std      28.4828   28.4828    28.4828   28.4828   28.4828
           weight  0.249841  0.249841  0.0227897  0.227687  0.249841
2006-05-02 price      72.34     72.34      6.669     66.55     72.34
           std      28.8308   28.8308    28.8308   28.8308   28.8308
           weight  0.249243  0.249243  0.0229776  0.229294  0.249243
2006-05-03 price      70.22     70.22      6.662     66.46     70.22
           std      28.0509   28.0509    28.0509   28.0509   28.0509
           weight  0.247443  0.247443  0.0234758  0.234194  0.247443
2006-05-04 price      68.32     68.32      6.758     67.48     68.32
           std      27.4399   27.4399    27.4399   27.4399   27.4399
           weight  0.244701  0.244701   0.024205  0.241692  0.244701
2006-05-05 price         68        68      6.805     67.99        68
           std      27.3661   27.3661    27.3661   27.3661   27.3661
           weight  0.243907  0.243907  0.0244086  0.243871  0.243907
2006-05-08 price      67.88     67.88      6.768     67.56     67.88
           std      27.2947   27.2947    27.2947   27.2947   27.2947
           weight  0.244201  0.244201  0.0243481   0.24305  0.244201
</code></pre>

<p>As a side note, I am not sure what kind of std you want to calculate, so I just assumed it was the row-wise price std (which will be a single/repeated value for each row).</p>

python pandas dataframe apply