I have a dataframe as follows:
CAR MAKER MODEL 0 1
CYL 4 6 4 6
HONDA CIVIC 5 0 0 0
TOYOTA HLAND 0 0 10 15
HONDA PILOT 0 0 10 2
TOYOTA COROLLA 0 5 5 4
The df is an MultiIndex with CAR having 0 and 1 and for each CAR there are 4 and 6 as possibilities for CYL
I want to arrange the df such that it results in the following output:
MAKER MODEL 0_4 0_6 1_4 1_6
HONDA CIVIC 5 0 0 0
TOYOTA HLAND 0 0 10 15
HONDA PILOT 0 0 10 2
TOYOTA COROLLA 0 5 5 4
I have tried to reset_index(col_level=2) but no success so far.
Any ideas on which method I can use to reorganize the df?
Its essentially the reverse of the https://pandas.pydata.org/docs/reference/api/pandas.MultiIndex.from_frame.html#pandas.MultiIndex.from_frame
df.columns is:
MultiIndex([( 'MAKER', ''),
('MODEL', ''),
( 0, 4),
( 0, 6),
( 1, 4),
( 1, 6)],
names=['CAR', 'CYL'])
Thanks!
df.to_dict? It's not very clear ifMAKER MODELare two columns or index.df.to_dictbut here isdf.columnsdf.head(6).to_dict()is enough.