pandas MultiIndex with duplicate values in one level

I need to replace the values of one level of pandas MultiIndex with another set of values. This is easily done with set_levels unless the new set contains duplicates, i.e. two or more values from the original set of level values are mapped to the same value in the new set. Doing so results in error. There is a brute force way of making this work by getting all index values (instead of levels) as MultiIndex of tuples, mapping to the new level in this space, then replacing the original index with a new MultiIndex constructed with from_tuples method. Is there a better way?

Read more here:

Content Attribution

This content was originally published by skypi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: