pandas 从数据框中删除顶行

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

Remove top row from a dataframe

pythonpandasdataframe

提问by SBad

I have a dataframe that looks like this:

我有一个看起来像这样的数据框:

         level_0              level_1 Repo Averages for 27 Jul 2018
0  Business Date           Instrument                           Ccy
1     27/07/2018  GC_AUSTRIA_SUB_10YR                           EUR
2     27/07/2018    R_RAGB_1.15_10/18                           EUR
3     27/07/2018    R_RAGB_4.35_03/19                           EUR
4     27/07/2018    R_RAGB_1.95_06/19                           EUR

I am trying to get rid of the top row and only keep

我试图摆脱顶行,只保留

   Business Date           Instrument         Ccy
0     27/07/2018  GC_AUSTRIA_SUB_10YR         EUR
1     27/07/2018    R_RAGB_1.15_10/18         EUR
2     27/07/2018    R_RAGB_4.35_03/19         EUR
3     27/07/2018    R_RAGB_1.95_06/19         EUR

I tried df.columns.droplevel(0)but not successful any help is more than welcome

我试过df.columns.droplevel(0)但没有成功任何帮助都非常受欢迎

采纳答案by Joe

You can try so:

你可以试试:

df.columns = df.iloc[0]
df = df.reindex(df.index.drop(0)).reset_index(drop=True)
df.columns.name = None

Output:

输出:

  Business Date           Instrument  Ccy
0    27/07/2018  GC_AUSTRIA_SUB_10YR  EUR
1    27/07/2018    R_RAGB_1.15_10/18  EUR
2    27/07/2018    R_RAGB_4.35_03/19  EUR
3    27/07/2018    R_RAGB_1.95_06/19  EUR