Skip to content
Advertisement

pandas MultiIndex: insert a row at top and keep first level of index hidden

In MultiIndex dataframe:

JavaScript

I want to insert a row ex at top and keep the first level of MultiIndex hidden. The expected result is

enter image description here

I tried concat:

JavaScript

However it become

enter image description here

I also tried first concat without index, then groupby multiply index. But pandas will automatically sort by MultiIndex. As a result, ex row cannot be set at top.

Advertisement

Answer

You need pass the correct index format

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement