Skip to content

How to best add or remove levels from a multiindex? #5991

Answered by benbovy
aspitarl asked this question in Q&A
Discussion options

You must be logged in to vote

You actually don't need to first reset the index.

You can add a level with set_index(..., append=True):

mda_new = mda.assign_coords(three=('x', [1,2,3,4,5,6])).set_index(x='three', append=True)

Or remove a level with mda_new.reset_index(x="one", drop=True)

However, I wouldn't rely too much on that. Xarray indexes are currently being refactored and the API above might be depreciated eventually.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mathause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants