Splitting stacked dataframe in pandas
<p>I have a dataframe like</p>

<pre><code>         age sex      values
time                        
2015      10   F    589628.0
2015      10   M    458390.0
2015      11   F    108018.0
2015      11   M    764350.0
 ....
2000      60   M     34676.0
2000      60   F     45488.0
</code></pre>

<p>I would like to create data frame like</p>

<pre><code>         age   F           M
time                        
2015      10   589628.0  458390.0    
2015      11   
 ....
2000      60   45488.0   34676.0
</code></pre>

<p>reducing the rows by half and adding a column. I have tried to do this with pivot, but no avail.</p>

<pre><code>df.pivot(columns='sex', values='values') 
</code></pre>

<p>but this returns</p>

<pre><code>Index contains duplicate entries, cannot reshape
</code></pre>

<p>Any ideas how can I cleanly split the dataframe without writing a tedious function to do it?</p>

<p>Cheers, Mike</p>

<p>I can't confirm this but it should be</p>

<pre><code>df.set_index(['age', 'sex'], append=True)['values'].unstack().reset_index('age')
</code></pre>

python pandas dataframe