I can create a df and then modify it to have a two level column index as follows:
import pandas as pd
import numpy as np
idx = pd.MultiIndex.from_product([['bar', 'baz', 'foo', 'qux'], ['one', 'two', 'three']])
df = pd.DataFrame(np.random.randn(12, 6), index=idx, columns=['C', 'D', 'E', 'F', 'G', 'H'])
print(df, '\n')
df.columns = pd.MultiIndex.from_product([['A'], df.columns])
print(df.head(3))
I get this:
A
C D E F G H
bar one -0.370228 1.246188 0.673553 0.116890 0.129511 0.126562
two -1.059752 -0.357985 -0.189913 1.080814 0.588176 0.212053
three -0.345277 -1.227097 0.915477 1.475285 -1.342885 0.149785
But what I want is this:
A B
C D E F G H
bar one -0.370228 1.246188 0.673553 0.116890 0.129511 0.126562
two -1.059752 -0.357985 -0.189913 1.080814 0.588176 0.212053
three -0.345277 -1.227097 0.915477 1.475285 -1.342885 0.149785
So that my columns are accessed as: AC, AD, AE, BF, BG, BH
I have tried this (and a few other things):
df.columns = pd.MultiIndex.from_product([[['A'], df[['C', 'D', 'E']]], [['B'], df[['F', 'G', 'H']]]])
But I keep getting this error:
TypeError: unhashable type: 'list'
How can I create the multi-index in the way that I desire?