Make multiindex columns in a pandas dataframe
<p>I have a pandas dataframe with the following strcuture:</p>

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

df = pd.DataFrame(np.arange(32).reshape((4,8)), 
            index = pd.date_range('2016-01-01', periods=4),
            columns=['male ; 0', 'male ; 1','male ; 2','male ; 4','female ; 0','female ; 1','female ; 2','female ; 3',])
</code></pre>

<p>The column names are messy with a combination of two variable in the header name, and residual punctuation from the original spreadsheet.</p>

<p>What I want to do is set a column MultiIndex called sex and age in my dataframe.</p>

<p>I tried using <code>pd.MultiIndex.from_tuples</code> like this:</p>

<pre><code>columns = [('Male', 0),('Male', 1),('Male', 2),('Male', 3),('Female', 0),('Female', 1),('Female', 2),('Female', 3)]
df.columns = pd.MultiIndex.from_tuples(columns)
</code></pre>

<p>And then naming the column indexes:</p>

<pre><code>df.columns.names = ['Sex', 'Age']
</code></pre>

<p>This gives the result that I would like. However, my dataframes has ages to over 100 for each sex so this is not very practical.</p>

<p>Could someone please guide me on how to set MultiIndex columns from a tuple programatically.</p>