Skip to content Skip to sidebar Skip to footer

How Can I Add A Level To A Multiindex?

index = [np.array(['foo', 'foo', 'qux']), np.array(['a', 'b', 'a'])] data = np.random.randn(3, 2) columns = ['X', 'Y'] df = pd.DataFrame(data, index=index, columns=columns

Solution 1:

The simpliest is use stack with new DataFrame with columns by new level values:

df1 = pd.DataFrame(data=1,index=df.index, columns=new_level_labels).stack()
df1.index.names = ['Level0','Level1',new_level_name]
print (df1)
Level0  Level1  New level
foo     a       p            1
                q            1
        b       p            1
                q            1
qux     a       p            1
                q            1
dtype: int64

print (df1.index)
MultiIndex(levels=[['foo', 'qux'], ['a', 'b'], ['p', 'q']],
           labels=[[0, 0, 0, 0, 1, 1], [0, 0, 1, 1, 0, 0], [0, 1, 0, 1, 0, 1]],
           names=['Level0', 'Level1', 'New level'])

Solution 2:

Not exactly sure what data should be in your desired DataFrame, but for a similar problem I used melt():

df1 = df.reset_index()
pd.melt(df1, id_vars=['Level0', 'Level1'])


    Level0  Level1  variable    value
0   foo a   X   0.678564
1   foo b   X   -0.609134
2   qux a   X   0.505178
3   foo a   Y   -2.593380
4   foo b   Y   -0.232796
5   qux a   Y   -1.420875

Post a Comment for "How Can I Add A Level To A Multiindex?"