32

I have this Dataframe:

import pandas as pd
df = pd.DataFrame({'Hugo' : {'age' : 21, 'weight' : 75},
                   'Bertram': {'age' : 45, 'weight' : 65},
                   'Donald' : {'age' : 75, 'weight' : 85}}).T
df.index.names = ['name']


         age  weight
name                
Bertram   45      65
Donald    75      85
Hugo      21      75

I want to change the index to the column 'age':

df.set_index('age', inplace=True)

     weight
age        
45       65
75       85
21       75

The old index-column name gets lost. Is there a way to change the index without losing the original index-column and getting the old column as 'normal' column again, so that it looks like this?

     name       weight
age        
45   Bertram    65
75   Donald     85
21   Hugo       75
0

5 Answers 5

40

Use reset_index first and then set_index:

df = df.reset_index().set_index('age')
print (df)
        name  weight
age                 
45   Bertram      65
75    Donald      85
21      Hugo      75
Sign up to request clarification or add additional context in comments.

Comments

12

Adding the append=True and with reset_index

df.set_index('age', append=True).reset_index(level=0)
Out[80]: 
        name  weight
age                 
45   Bertram      65
75    Donald      85
21      Hugo      75

3 Comments

This might be slightly more efficient than the accepted answer.
Nah, in this specific case at least it's twice as slow. I'm curious though, why do you think it might be faster?
this is slower in my testing
8

Your DataFrame df has name (= 'Bertram', 'Donald', 'Hugo') as index

That is, your df is:

         age  weight
name                
Bertram   45      65
Donald    75      85
Hugo      21      75

You can convert the index (name) into a new column inside your DataFrame df by using the .reset_index() method.

df.reset_index(inplace=True)

name becomes a column and the new index is the standard default integer index:

Your df looks like this now:

Out[1]:    
    name     age  weight

0   Bertram   45      65
1   Donald    75      85
2   Hugo      21      75

Now, you can change the index to age with the .set_index() method.

df.set_index('age',inplace=True)

dfis now:

Out[2]: 
     name  weight
age                 
45   Bertram      65
75   Donald       85
21   Hugo         75

As @jezrael points out above you can do this in a single step, instead of two steps, like this:

df = df.reset_index().set_index('age')

Comments

5

Change the drop variable to False.

df = df.set_index("age", drop=False)

3 Comments

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
We need to use reset_index(level = 0). Your answer does not work
This doesn't keep the old index from being dropped, it keeps the column used as the new index from being dropped.
-1

The below is the most efficient since it appends the new index of age and makes sure its inplace

df.set_index('age',append=True,inplace=True)

1 Comment

We need to use reset_index(level = 0). Your answer does not work

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.