Can't change column name in python using pandas library

Question:

enter image description here

data set

                     Age
  Shoes Name    
  Nike Dunks       23.000000
  Nike Jordan      21.666667

rename column name function

 df4=df3.rename(columns = {'Shoes Name':'catagory_of_shoes','Age': 'avg_age_group'})
 df4.head()

output

            avg_age_group
Shoes Name  
Nike Dunks     23.000000
Nike Jordan    21.666667

It’s not changing the 1st column name.

Asked By: Khalilomorph

||

Answers:

The 1st column isn’t a column, it is the index. See pd.rename (link) for details of how to rename the index

Answered By: Mouse

You can use df.index.name to access the index name.

df.index.name = 'catagory_of_shoes'

Or you can reset the index to column

df4 = (df3.reset_index()
       .rename(columns={'Shoes Name': 'catagory_of_shoes',
                        'Age': 'avg_age_group'}))
Answered By: Ynjxsjmh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.