(possibly grouped) Row Values to Columns
<p>Let's say, after some groupby operation I have a dataframe like this:</p>

<pre><code>data = pd.DataFrame(columns=['Key', 'Subkey', 'Value'])
data.loc[0] = ['foo1', 'bar1', 20]
data.loc[1] = ['foo1', 'bar2', 10]
data.loc[2] = ['foo1', 'bar3', 5]
data.loc[3] = ['foo2', 'bar1', 50]
data.loc[4] = ['foo2', 'bar2', 100]
data.loc[5] = ['foo2', 'bar3', 50]
</code></pre>

<p>What I then have is a dataframe that looks like this:</p>

<pre><code>|Key |Subkey | Value |
+----+-------+-------+
|foo1|bar1   |20     |
|foo1|bar2   |10     |
|foo1|bar3   |5      |
|foo2|bar1   |50     |
|foo2|bar2   |100    |
|foo2|bar3   |50     |
</code></pre>

<p>What I would like to have is a new dataframe where the subkey is a new column, containing the same value as in the grouped frame above, like:</p>

<pre><code>|Key |bar1 |bar2  |bar3  |
+----+-----+------+------+
|foo1| 20  |  10  | 5    |
|foo2| 50  | 100  | 50   |
</code></pre>

<p>Is there a one-line solution to this, or do I need to transform the dataframe programmatically?</p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.pivot.html" rel="nofollow"><code>pivot</code></a>:</p>

<pre><code>print (data.pivot(index='Key', columns='Subkey', values='Value'))
Subkey  bar1   bar2  bar3
Key                      
foo1    20.0   10.0   5.0
foo2    50.0  100.0  50.0
</code></pre>

<p>Then you can cast <code>float</code> values to <code>int</code>, <code>reset_index</code> and remove column names <code>Subkey</code>:</p>

<pre><code>print (data.pivot(index='Key', columns='Subkey', values='Value')
           .astype(int)
           .reset_index()
           .rename_axis(None, axis=1))

    Key  bar1  bar2  bar3
0  foo1    20    10     5
1  foo2    50   100    50
</code></pre>

python pandas