Aug-07-2019, 11:27 PM
Suppose you have a data frame with multiindex, e.g.
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second']) tuples = list(zip(*arrays)) df = pd.DataFrame(pd.np.random.randn(8), index=index)Then if you call
df.reset_index()
, you get almost what you want:Output: first second 0
0 bar one 0.602861
1 bar two 1.843366
2 baz one 0.425292
3 baz two -1.067120
4 foo one 1.012777
5 foo two 0.607141
6 qux one 0.582811
7 qux two 2.501628
Something like df.reset_index().to_excel('...')
should give you desired result.