pandas 删除PANDAS中标题的第二行

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

Delete second row of header in PANDAS

pythonpandasdataframe

提问by km1234

I have a dataframe in PANDAS which has two lines of headers.How could I remove the second line? For example, I have the following:

我在 PANDAS 中有一个数据框,它有两行标题。如何删除第二行?例如,我有以下内容:

         AA  BB  CC  DD
         A   B   C   D
Index    
   1     1   2   3   4
   2     5   6   7   8
   3     9   1   2   3

and I would like to get something like this:

我想得到这样的东西:

         AA  BB  CC  DD
Index    
   1     1   2   3   4
   2     5   6   7   8
   3     9   1   2   3

Thank you very much.

非常感谢。

回答by jezrael

You can use droplevelwith -1: last level:

你可以用droplevel-1去年的水平:

df.columns = df.columns.droplevel(-1)
print df
       AA  BB  CC  DD
Index                
1       1   2   3   4
2       5   6   7   8
3       9   1   2   3

Or specify second level: 1:

或指定第二级1

df.columns = df.columns.droplevel(1)
print df
       AA  BB  CC  DD
Index                
1       1   2   3   4
2       5   6   7   8
3       9   1   2   3