且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设置pandas DataFrame的索引名称

更新时间:2022-11-14 09:01:47

如果配料是索引的名称,则可以通过以下方式进行设置:

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)