How to create hierarchical columns in pandas?
<p>I have a pandas dataframe that looks like this:</p>

<pre><code>          rank_2015   num_2015   rank_2014   num_2014   ....  num_2008
France    8           1200       9           1216       ....  1171
Italy     11          789        6           788        ....  654
</code></pre>

<p>Now I want to draw a bar chart of the sums just the <code>num_</code> columns, by year. So on the x-axis I would like years from 2008 to 2015, and on the y-axis I would like the sum of the related <code>num_</code> column. </p>

<p>What's the best way to do this? I know how to get the sums for each column:</p>

<pre><code>df.sum()
</code></pre>

<p>But what I don't know is how to chart only the <code>num_</code> columns, and also how to re-label those columns so that the labels are integers rather than strings, in order to get them to chart correctly. </p>

<p>I'm wondering if I want to create hierarchical columns, like this:</p>

<pre><code>          rank               num
          2015        2014   2015     2014   ....  2008
France    8           9      1200     1216   ....  1171
Italy     11          6      789      788    ....  654
</code></pre>

<p>Then I could just chart the columns in the <code>num</code> section. </p>

<p>How can I get my dataframe into this shape?</p>

<p>Probably you don't need re-shaping your dataset, it can be achieved easier.</p>

<ol>
<li>Create new dataset, which contains <code>num_</code> data only</li>
<li>Rename columns</li>
<li>Plot sum</li>
</ol>

<p>Dummy data:</p>

<p><a href="http://i.stack.imgur.com/gzPeAm.png" rel="nofollow"><img src="http://i.stack.imgur.com/gzPeAm.png" alt="enter image description here"></a></p>

<p>Code:</p>

<pre><code>df_num = df[[c for c in df.columns if c.startswith('num_')]]
df_num.columns = [c.lstrip('num_') for c in df_num.columns]
df_num.sum().plot(kind='bar')
</code></pre>

<p>Result:</p>

<p><a href="http://i.stack.imgur.com/0KsvVm.png" rel="nofollow"><img src="http://i.stack.imgur.com/0KsvVm.png" alt="enter image description here"></a></p>

python pandas