DEV Community

Nilotpal Choudhury
Nilotpal Choudhury

Posted on

Answer: pandas add higher level column multiindex

I believe you need in first step convert first level to date values and then reshape by DataFrame.unstack, MultiIndex.reorder_levels and DataFrame.sort_index:

date = pd.Timestamp('today').floor('d')
df.index = pd.MultiIndex.from_product([[date], df.index.get_level_values(1)])
df = df.unstack().reorder_levels([2,0,1], axis=1).sort_index(axis=1)
print (df)
company    Bank X                        Bank Y                        Bank Z  \
product         A        B        C           A        B
…

Top comments (0)