Set index name of pandas DataFrame

自闭症网瘾萝莉.ら 提交于 2020-07-15 01:00:31

问题


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
...

回答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)



回答2:


Try this:

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


来源:https://stackoverflow.com/questions/37968730/set-index-name-of-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!