You need to unpin the existing index level Foo , stack the desired column "Coo", and then change the index levels. After exchanging indexes, you probably want to sort it. As a final touch, you may need to remove the column name of all values ββ( val ).
df = (pd.DataFrame({'Foo': [124, 124, 134, 134] * 2, 'Bar': [1, 2, 1, 2] * 2, 'Coo': ['BAZ'] * 4 + ['PAL'] * 4, 'val': list('ACEGBDFH')}) .set_index(['Foo', 'Bar', 'Coo']) .unstack('Coo')) >>> df val Coo BAZ PAL Foo Bar 124 1 AB 2 CD 134 1 EF 2 GH df = df.unstack('Foo').stack('Coo') df.index = df.index.swaplevel(0, 1) >>> df val Foo 124 134 Coo Bar BAZ 1 AE PAL 1 BF BAZ 2 CG PAL 2 DH df.sort_index(inplace=True) >>> df val Foo 124 134 Coo Bar BAZ 1 AE 2 CG PAL 1 BF 2 DH df.columns = df.columns.droplevel() >>> df Foo 124 134 Coo Bar BAZ 1 AE 2 CG PAL 1 BF 2 DH
source share