Set index name of pandas DataFrame

Question:

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: ‘ingredient’.

I created the dataframe from a csv file:

df = pd.read_csv('./data/file_name.csv', index_col=False, encoding="ISO-8859-1")  
df = df['ingredient_group']  #selecting column 
df = df.value_counts()       #calculating string occurance which return series obj
df = pd.DataFrame(df)        #creating dataframe from series obj

How do I assign the name ‘ingredient’ to the first column which has currently no name?

I already tried:

df_count.rename(columns={'': 'ingredient'}, inplace=True)

df = pd.DataFrame(df, columns = ['ingredient','count']

How do I prevent this from happening?

''        count
ingredient  ''
sugar      420
milk       108
vanilla    450
...
Asked By: Afke

||

Answers:

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)
Answered By: user3404344

Try this:

cols_ = df.columns
cols[0] = 'ingredient'
df.columns = cols_
Answered By: Dmitry Andreev

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

df.rename_axis('ingredient', inplace=True)
Answered By: HoG
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.