2

I have a MultiIndex Dataframe df like Below

                 Office
 Office    
 x         True   2
 y         False  3
 z         True   5

If i reset df.reset_index() it will Error as

"cannot insert Office, already exists"

How its posible to rename the the higher(which comes in very first line) index name "Office" to "Office1"

1 Answer 1

4

You can use rename_axis or set index.names for rename index names in MultiIndex and rename for change column name:

#if only rename get Unnamed column
df1 = df.rename(columns={'Office':'another col'}).reset_index()
print (df1)
  Office  Unnamed: 1  another col
0      x        True            2
1      y       False            3
2      z        True            5

df2 = df.rename_axis(('Office', 'bool')).rename(columns={'Office':'Office2'}).reset_index()
print (df2)
  Office   bool  Office2
0      x   True        2
1      y  False        3
2      z   True        5

df.index.names = ('Office1','bool')
df3 = df.rename(columns={'Office':'Office2'}).reset_index()
print (df3)
  Office1   bool  Office2
0       x   True        2
1       y  False        3
2       z   True        5
Sign up to request clarification or add additional context in comments.

4 Comments

reset_index() got an unexpected keyword argument 'name'
Sorry, I miss MultiIndex first.
How to change the Name of column name office having values 2,3,5 to office1 not the other one
Do you think rename? I add it to answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.