Skip to content
Advertisement

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

In MultiIndex dataframe:

import pandas as pd
a = [['a', 'b', 2, 3], ['c', 'b', 5, 6], ['a','c', 8, 9]]
df = pd.DataFrame(a, columns=['I1', 'I2', 'v1', 'v2'])
df = df.groupby(['I1', 'I2']).first()

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:

data_ex = {'v1':[99], 'v2': [98]}
df_ex = pd.DataFrame(data_ex, index = [('ex','ex')])
pd.concat([df_ex, df])

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

df_ex = pd.DataFrame(data_ex, index = pd.MultiIndex.from_tuples([('ex','ex')],names=["I1", "I2"]))

pd.concat([df_ex, df])
Out[783]: 
       v1  v2
I1 I2        
ex ex  99  98
a  b    2   3
   c    8   9
c  b    5   6
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement