python Numpy transpose and calculate
<p>I am relatively new to python and numpy. I am currently trying to replicate the following table as shown in the image in python using numpy.</p>

<p><a href="http://i.stack.imgur.com/nVKbb.png" rel="nofollow"><img src="http://i.stack.imgur.com/nVKbb.png" alt="enter image description here"></a>
As in the figure, I have got the columns "group, sub_group,value" that are populated. I want to transpose column "sub_group" and do a simple calculation i.e. value minus shift(value) and display the figure in the lower diagonal of the matrix for each group. If sub_group is "0", then assign the whole column as 0. The <strong>transposed sub_group</strong> can be named anything (preferably index numbers) if it makes it easier. I am ok with a pandas solution as well. I just think pandas may be slow?</p>

<p>Below is code in array form:</p>

<pre><code>import numpy as np
a=np.array([(1,-1,10),(1,0,10),(1,-2,15),(1,-3,1),(1,-4,1),(1,0,12),(1,-5,16)], dtype=[('group',float),('sub_group',float),('value',float)])
</code></pre>

<p>Any help would be appreciated.
Regards,
S</p>

<p>Try this out :</p>

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

a=np.array([(1,-1,10),(1,0,10),(1,-2,15),(1,-3,1),(1,-4,1),(1,0,12),(1,-5,16)], dtype=[('group',float),('sub_group',float),('value',float)])
df = pd.DataFrame(a)

for i in df.index:
    col_name = str(int(df['sub_group'][i]))
    df[col_name] = None

    if df['sub_group'][i] == 0:
        df[col_name] = 0
    else:
        val = df['value'][i]
        for j in range(i, df.index[-1]+1):
            df[col_name][j] = val - df['value'][j]
</code></pre>

<p>For the upper triangle of the matrix, I have put <code>None</code>values. You can replace it by whatever you want.</p>

python arrays pandas numpy