Search code examples
pythonpandasdataframemulti-index

Assigning columns to a hierarchical higher Multiindex level


I have a dataframe that looks somewhat like this:

   Ax  Ay  Bx  By
0   1  20   2  20
1   3  21   5  22
2   4  20   7  25

and I want to manually define that Ax and Ay have the hierarchical higher MultiIndex level of A (and also do the same for B). So that the dataframe looks like this:

   A      B
   x   y  x   y
0  1  20  2  20
1  3  21  5  22
2  4  20  7  25

So what I'm looking for is a statement that says:

Ax and Ay go under A as x and y


Solution

  • You can manually construct a pandas.MultiIndex using one of several constructors. From the docs:

    All of these are useful and I'd check them each out to understand their use cases. For your case, I think pd.MultiIndex.from_tuples might do the trick:

    In [4]: list_of_split_tuples = list(map(tuple, df.columns.values))
    
    In [5]: df.columns = pd.MultiIndex.from_tuples(list_of_split_tuples)
    
    In [6]: df
    Out[6]:
       A      B
       x   y  x   y
    0  1  20  2  20
    1  3  21  5  22
    2  4  20  7  25