Renaming a single level item in pandas MultiIndex

I have a pandas DataFrame with a structure like this:

Name Location Position Data1 Data2 Foo loc1 12345 present absent Foo2 loc2 67890 absent present 

where Name , Location and Position are MultiIndex levels. However, I need to rename one index level: for this example, I need to change Foo2 to Bar .

I thought DataFrame.rename() would do the job, however, if I specify

 new_df = old_df.rename(index={"Foo2": "Bar"}) 

it really does not change the index, and, even worse, it aligns it.

Is this what I want to do? Or do I find a pandas error?

+4
source share
1 answer

The rename function must convert the dictionary to a cartographer and apply it to each index. However, for the case of MultiIndex it passes only for each tuple, but not for each index. I fixed it and made a transfer request .

+4
source

All Articles