Apply a weight formula over a Dataframe using Numpy
<p>I have a Dataframe and am looking to divide the float value of a cell by the sum of the row where it resides. For this I use a numpy formula. This therefore would give me a weight for that cell for that row. I have this dataframe <code>df1</code>:</p>

<pre class="lang-none prettyprint-override"><code>                           AA          AB             AC           AD  
2005-01-02 23:55:00      "EQUITY"    "EQUITY"      "EQUITY"     "EQUITY"   
2005-01-03 00:00:00        32.32      19.5299        32.32      31.0455   
2005-01-04 00:00:00      31.9075      19.4487      31.9075      30.3755   
2005-01-05 00:00:00      31.6151      19.5799      31.6151       29.971   
2005-01-06 00:00:00      31.1426      19.7174      31.1426      29.9647  
</code></pre>

<p>I have tried the following so far:</p>

<pre><code>import numpy as np

def foo_bar(x):
    if type(x) is not str:
        return x / np.sum(df1, axis=1)
    else:
        return

df = df_numeric.apply(np.vectorize(foo_bar))
</code></pre>

<p>It seems the sum part of my formula is not properly written as I get the following error:</p>

<pre class="lang-none prettyprint-override"><code>                           AA          AB             AC           AD  
ValueError: ('setting an array element with a sequence.', 'occurred at index AA')
</code></pre>

<p>The message error is quite informative in this case: <strong>you are trying to set an array element (<em>x</em>) with a sequence.</strong></p>

<p>Try to load your dataframe <code>df1</code> in a Python prompt and print the expression <code>np.sum(df1,axis=1)</code>: it returns a sequence -a vector- containing the sums of each row. You would need to select the element of this sequence which corresponds to the appropriate row in each case.</p>

<p>You can <strong>try the following</strong>, as explained <a href="http://stackoverflow.com/questions/18594469/normalizing-a-pandas-dataframe-by-row">here</a>:</p>

<p><code>df1.div(df1.sum(axis=1), axis=0)</code></p>

<p><code>div</code> will apply element-wise division in your dataframe.</p>

<p>From your <code>df1</code> snippet it seems that you have rows with strings (<code>"EQUITY"</code>) alternated with rows with numbers, those will give you problems. I suggest you take out any string rows and leave only a dataframe with numbers. You can use the column titles to give significant names to the columns in your dataframe.</p>

python pandas numpy dataframe