python pandas: Reallocate index, columns and values within dataframe
<p>I have a dataframe that looks as follows. x is the index</p>

<pre><code>                  y      value
x                   
1                 0  0.016175
1                 1  0.017832
1                 2  0.021536
1                 3  0.024777
2                 0  0.027594
2                 1  0.029950
2                 2  0.031890
2                 3  0.033570
3                 0  0.035070
3                 1  0.036329
3                 2  0.037297
3                 3  0.037983
</code></pre>

<p>I would like to reallocate the data in the frame so that the result looks like:</p>

<pre><code>                 y       1(x)       2(x)         3(x)

                 0  0.016175    0.027594    0.035070
                 1  0.017832    0.029950    0.036329
                 2  0.021536    0.031890    0.037297
                 3  0.024777    0.033570    0.037983
</code></pre>

<p>The original index should be placed as column headings and y should be the new index. Any ideas how to implement this in Python?</p>

<p>You can use first <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reset_index.html" rel="nofollow"><code>reset_index</code></a>, then <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.pivot.html" rel="nofollow"><code>pivot</code></a> and last <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.add_suffix.html" rel="nofollow"><code>add_suffix</code></a>:</p>

<pre><code>print (df.reset_index().pivot(index='y', columns='x', values='value').add_suffix('(x)'))
x      1(x)      2(x)      3(x)
y                              
0  0.016175  0.027594  0.035070
1  0.017832  0.029950  0.036329
2  0.021536  0.031890  0.037297
3  0.024777  0.033570  0.037983
</code></pre>

<p>Last if need remove column names add <a href="http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#changes-to-rename" rel="nofollow"><code>rename_axis</code></a> (new in <code>pandas</code> <code>0.18.0</code>):</p>

<pre><code>print (df.reset_index()
         .pivot(index='y', columns='x', values='value')
         .add_suffix('(x)')
         .rename_axis(None, axis=1))
       1(x)      2(x)      3(x)
y                              
0  0.016175  0.027594  0.035070
1  0.017832  0.029950  0.036329
2  0.021536  0.031890  0.037297
3  0.024777  0.033570  0.037983
</code></pre>

python pandas dataframe pivot