Pandas dataframe隐藏索引功能?

J G*_*rif 19 python pandas ipython-notebook

显示pandas数据帧时是否可以隐藏索引,以便只有列名出现在表的顶部?

这需要适用于ipython notebook中的html表示和to_latex()函数(我正在使用nbconvert).

助教.

Tho*_*mas 26

正如@waitingkuo所指出的那样,index = False就是你所需要的.如果你想在你的ipython笔记本中保留漂亮的表格布局,你可以使用:

from IPython.display import display, HTML
display(HTML(df.to_html(index=False)))
Run Code Online (Sandbox Code Playgroud)


wai*_*kuo 22

index=False

对于ipython笔记本:

print df.to_string(index=False)
Run Code Online (Sandbox Code Playgroud)

对于to_latex:

df.to_latex(index=False)
Run Code Online (Sandbox Code Playgroud)

  • @WillemvanDoesburg:`来自IPython.display导入HTML HTML(df.to_html(index = False))` (5认同)

Ste*_*o M 11

从0.17.1版开始,可以通过样式隐藏索引,请参见隐藏索引或列:如果df您的Data Frame只是在做

df.style.hide_index()
Run Code Online (Sandbox Code Playgroud)

  • `.style` 方法中断了对 Latex/pdf 的转换,仅渲染 `<pandas.io.formats.style.Styler at 0x......>`。 (2认同)

que*_*o42 7

尝试

df.style.hide(axis="index")
Run Code Online (Sandbox Code Playgroud)

否则你会看到:

FutureWarning: this method is deprecated in favour of `Styler.hide(axis="index")`
  df.loc[df.index,['name','depth']].style.hide_index()
Run Code Online (Sandbox Code Playgroud)

请参阅DEPR REF:hide(axis=..) 替换 hide_index 和 hide_columns #43771


Wil*_*urg 5

我将以下单元格添加到我的笔记本中,它在 Jupyter 4.0.2 中运行良好。

注意:即使没有索引,它也会删除“任何”表的第一列。

# Execute this cell to remove the first column of dataframe tables (to remove index column)
from IPython.core.display import HTML
HTML("""
<style>
    table.dataframe thead th:first-child {
        display: none;
    }
    table.dataframe tbody th {
        display: none;
    }
</style>
""")
Run Code Online (Sandbox Code Playgroud)