Python 删除熊猫中的索引名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29765548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:58:23  来源:igfitidea点击:

Remove index name in pandas

pythonpandasindexingrowname

提问by markov zain

I have a dataframe like this one:

我有一个这样的数据框:

In [10]: df
Out[10]: 
         Column 1
foo              
Apples          1
Oranges         2
Puppies         3
Ducks           4

How to remove index namefoofrom that dataframe? The desired output is like this:

如何index namefoo从该数据框中删除?所需的输出是这样的:

In [10]: df
Out[10]: 
         Column 1             
Apples          1
Oranges         2
Puppies         3
Ducks           4

采纳答案by S Anand

Use del df.index.name

del df.index.name

In [16]: df
Out[16]:
         Column 1
foo
Apples          1
Oranges         2
Puppies         3
Ducks           4

In [17]: del df.index.name

In [18]: df
Out[18]:
         Column 1
Apples          1
Oranges         2
Puppies         3
Ducks           4

回答by EdChum

Alternatively you can just assign Noneto the index.nameattribute:

或者,您可以只分配Noneindex.name属性:

In [125]:

df.index.name = None
df
Out[125]:
         Column 1

Apples          1
Oranges         2
Puppies         3
Ducks           4

回答by jezrael

From version 0.18.0you can use rename_axis:

从版本0.18.0你可以使用rename_axis

print df
         Column 1
foo              
Apples          1
Oranges         2
Puppies         3
Ducks           4

print df.index.name
foo


print df.rename_axis(None)
         Column 1
Apples          1
Oranges         2
Puppies         3
Ducks           4

print df.rename_axis(None).index.name
None

# To modify the DataFrame itself:
df.rename_axis(None, inplace=True)
print df.index.name
None