I have a dataframe with Multiindex and would like to modify one particular level of the Multiindex. For instance, the first level might be strings and I may want to remove the white spaces from that index level:
df.index.levels[1] = [x.replace(' ', '') for x in df.index.levels[1]]
However, the code above results in an error:
TypeError: 'FrozenList' does not support mutable operations.
I know I can reset_index and modify the column and then re-create the Multiindex, but I wonder whether there is a more elegant way to modify one particular level of the Multiindex directly.
As mentioned in the comments, indexes are immutable and must be remade when modifying, but you do not have to use
reset_index
for that, you can create a new multi-index directly:This example is for a 3-level index, where you want to modify the middle level. You need to change the size of the tuple for different level sizes.
Thanks to @cxrodgers's comment, I think the fastest way to do this is:
Old, longer answer:
I found that the list comprehension suggested by @Shovalt works but felt slow on my machine (using a dataframe with >10,000 rows).
Instead, I was able to use
.set_levels
method, which was quite a bit faster for me.In actuality, I just needed to prepend some text. This was even faster with
.set_levels
:This solution is based on the answer in the link from the comment by @denfromufa ...
python - Multiindex and timezone - Frozen list error - Stack Overflow