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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay