Pandas - 删除列索引的标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53036910/
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-09-14 06:06:45  来源:igfitidea点击:

Pandas - remove the label of the column index

pythonpandasdataframe

提问by user9588528

I have a dataframe as follows:

我有一个数据框如下:

PLEASE_REMOVE  2013  2014  2015
 THIS_IS_EASY
-------------------------------
          Bob     0     3     4
         Mary     2     3     6

The years (2013, 2014, 2015) are the column index labels. The names (Mary, Bob) are the row index labels.

年份 (2013, 2014, 2015) 是列索引标签。名字 (Mary, Bob) 是行索引标签。

I have somehow managed to get labels for the row indices and column indices.

我以某种方式设法获得了行索引和列索引的标签。

Using df.index.names = ['']I can remove the THIS_IS_EASY bit.

使用df.index.names = ['']I 可以删除 THIS_IS_EASY 位。

How can I remove the PLEASE_REMOVE bit?

如何删除 PLEASE_REMOVE 位?

Desired output is:

期望的输出是:

               2013  2014  2015
 -------------------------------
          Bob     0     3     4
         Mary     2     3     6

回答by Peter Leimbigler

Simply delete the name of the columns:

只需删除列的名称:

del df.columns.name

Also, note that df.index.names = ['']is not quite the same as del df.index.name.

另请注意,df.index.names = ['']这与del df.index.name.

回答by Rocky Li

Like this:

像这样:

df = df.rename_axis(None)

This will get rid of everything on the top left. you can also do it in place: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename_axis.html

这将摆脱左上角的所有内容。你也可以就地做:https: //pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename_axis.html

回答by Ian Logie

In Pandas 1.0.3 the following works:

在 Pandas 1.0.3 中,以下工作:

df.columns.name = None

The 'accepted answer' above, del df.columns.name, leads to: 'AttributeError: can't delete attribute'

上面的“接受的答案”del df.columns.name导致:“AttributeError: can't delete attribute”