0

I have the following DF

a b
Test1 2 1
Test2 3 2

What I currently do is the following :

DF.reset_index().set_index('b')

which give the following output :

b a index
1 2 Test1
2 3 Test2

But what if I don't want the column to be named as "index" ?

For example I wish to have the following output : (Replace "index" by "TestName")

b a TestName
1 2 Test1
2 3 Test2
2
  • 1
    DF.reset_index().set_index('b').rename(columns={'index':'TestName'}). There's also rename_axis function as well. Commented Aug 11, 2022 at 16:08
  • Y i currently use DF.rename(columns={'index':'TestName'}, inplace=True) Commented Aug 11, 2022 at 16:09

3 Answers 3

1

You can use rename_axis:

DF.rename_axis('TestName').reset_index().set_index('b')
Sign up to request clarification or add additional context in comments.

Comments

0

You can just set the columns manually by doing this

df.columns = ['b', 'a', 'TestName']

Comments

0

Use index.rename()

DF.set_index('b', inplace=True)
DF.index.rename('TestName', inplace=True)
DF

1 Comment

Is there dataframe.index.rename() function?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.