Python Pandas 数据框隐藏索引功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21256013/
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
Pandas dataframe hide index functionality?
提问by J Grif
Is it possible to hide the index when displaying pandas dataframes, so that only the column names appear at the top of the table?
显示熊猫数据框时是否可以隐藏索引,以便只有列名出现在表的顶部?
This would need to work for both the html representation in ipython notebook and to_latex() function (which I'm using with nbconvert).
这需要同时适用于 ipython notebook 中的 html 表示和 to_latex() 函数(我与 nbconvert 一起使用)。
Ta.
塔。
采纳答案by waitingkuo
Set index=False
放 index=False
For ipython notebook:
对于 ipython 笔记本:
print df.to_string(index=False)
For to_latex:
对于 to_latex:
df.to_latex(index=False)
回答by Willem van Doesburg
I added the following cell to my notebook which works fine in Jupyter 4.0.2.
我将以下单元格添加到我的笔记本中,它在 Jupyter 4.0.2 中运行良好。
Note: It removes the first column of 'any' table even when there is no index.
注意:即使没有索引,它也会删除“任何”表的第一列。
# 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>
""")
回答by Thomas
As has been pointed out by @waitingkuo, index=False is what you need. If you want to keep the nice table layout within your ipython notebook, you can use:
正如@waitingkuo 指出的那样, index=False 是您所需要的。如果你想在你的 ipython notebook 中保留漂亮的表格布局,你可以使用:
from IPython.display import display, HTML
display(HTML(df.to_html(index=False)))
回答by Bhaskar Mitra
Set index=False.
设置index=False。
E.g: DataFrame.to_csv("filename", index=False)
例如: DataFrame.to_csv("filename", index=False)
This will work.
这将起作用。
回答by Stefano M
Starting from v. 0.17.1 it is possible to hide the index via styling, see hiding the index or colums: if dfis your Data Frame just do
从 v. 0.17.1 开始,可以通过样式隐藏索引,请参阅隐藏索引或列:如果df是您的数据框,请执行以下操作
df.style.hide_index()
Edit
编辑
Please note that styling works only in the notebook, and not within the LaTeX conversion.
请注意,样式仅适用于笔记本,而不适用于 LaTeX 转换。

