Skip to content Skip to sidebar Skip to footer

Set Index Name Of Pandas Dataframe

I have a pandas dataframe like this: '' count sugar 420 milk 108 vanilla 450 ... The first column has no header and I would like to give it the name: 'ingre

Solution 1:

if ingredients is the name of the index then you can set it by

df.index.name='ingredient'

With the current solutions you have 'ingredient' as name of the index, which is printed in different row to that of column names. This cannot be changed as is. Try the modified solution below, here the index is copied on to a new column with column name and the index replaced with sequence of numbers.

df['ingredient']=df.index
df = df.reset_index(drop=True)

Solution 2:

You are looking for how to set the name 'ingredient' of the AXIS for the Index.

df.rename_axis('ingredient', inplace=True)

Solution 3:

Try this:

cols_ = df.columns
cols[0] = 'ingredient'
df.columns = cols_

Post a Comment for "Set Index Name Of Pandas Dataframe"