如何在 Pandas 多级索引数据框中仅删除索引名称而不是内容

8
我有一个制表符分隔的文件(mydata.txt)如下:
Set Coolthing   Route   Organ   Up  Down
set4    foo ID  LN  81  60
set4    bar ID  LN  542 92
set4    foo ID  LV  73  73
set4    bar ID  LV  143 78
set4    foo ID  SP  32  82
set4    bar ID  SP  90  129

以下是相关代码:

import pandas as pd
df = pd.io.parsers.read_table("http://dpaste.com/3ZTTVQH.txt")
df = df.pivot(index="Coolthing",columns="Organ")
df.drop('Set',axis=1,inplace=True)
df.drop('Route',axis=1,inplace=True)

I have the following data frame:

In [15]: df
Out[15]:
            Up           Down
Organ       LN   LV  SP    LN  LV   SP
Coolthing
bar        542  143  90    92  78  129
foo         81   73  32    60  73   82

然后使用df.to_html(index=True, justify="left")创建这个HTML:

enter image description here

我想要做的是移除索引名称OrganCoolthing,得到以下结果:

             Up           Down
            LN   LV  SP    LN  LV   SP
bar        542  143  90    92  78  129
foo         81   73  32    60  73   82

我该如何实现这个目标?
2个回答

11

对于单层名称,有一个name和多层名称有names的称呼。因此,对于你的例子,你需要执行以下操作以清除索引和列中的名称:

In [372]:

df.index.name = None
df.columns.names = (None,None)
dfOut[372]:
      Up          Down         
      LN   LV  SP   LN  LV   SP
bar  542  143  90   92  78  129
foo   81   73  32   60  73   82

0
你可以通过它的名称属性来获取/设置索引。你可以做的是设置
    df.index.name = "" 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接