2

Task:

I would like to replace index values (i.e 0,1,2,3,4 etc.) with the values under Main, while removing Main as it no longer has use.

df:

                  Main             Ing 
0                  A              Apple            
1                  B              Bread            
2                  Z              Cheese            
3                  E              Egg            
4                  D              Dough           
5                  X              Pasta   

My Goal:

Output:

                Ing 
A              Apple            
B              Bread            
Z              Cheese            
E              Egg            
D              Dough           
X              Pasta   

I'm sure it's a pretty simple solution but I have no idea. Thanks in advance.

1 Answer 1

6

Use set_index:

df = df.set_index('Main')
print (df)
         Ing
Main        
A      Apple
B      Bread
Z     Cheese
E        Egg
D      Dough
X      Pasta

If need remove index name Main add rename_axis:

df = df.set_index('Main').rename_axis(None)
print (df)
      Ing
A   Apple
B   Bread
Z  Cheese
E     Egg
D   Dough
X   Pasta

Or set it to None:

df = df.set_index('Main')
df.index.name = None
print (df)
      Ing
A   Apple
B   Bread
Z  Cheese
E     Egg
D   Dough
X   Pasta
Sign up to request clarification or add additional context in comments.

Comments

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.