Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96300

Moving an index to a column

Say I have the following multi-index dataframe in Pandas:

X    Y       val
bar  one    -0.007381
baz  four    0.210000
foo  two     1.314373
qux  one     0.716789

Assuming that the second level only has one entry for each entry of the first level (BTW what do you call a MultiIndex or dataframe with this property?): How can I move one of the indices (e.g. Y) to make it a column? i.e.

X     val       Y
bar  -0.007381  one
baz   0.210000  four
foo   1.314373  two
qux   0.716789  one

Upvotes: 2

Views: 1883

Answers (1)

user1827356
user1827356

Reputation: 7022

The following would make Y the column

df.reset_index(level='Y')

If you want to make X the column, try this

df.reset_index(level='X')

Upvotes: 4

Related Questions