Home > Software engineering >  Can't change column name in python using pandas library
Can't change column name in python using pandas library

Time:07-30

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.

CodePudding user response:

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

CodePudding user response:

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'}))
  • Related