【发布时间】:2014-02-10 22:07:54
【问题描述】:
是否可以在显示 pandas 数据框时隐藏索引,以便仅列名出现在表的顶部?
这需要同时适用于 ipython notebook 中的 html 表示和 to_latex() 函数(我与 nbconvert 一起使用)。
Ta。
【问题讨论】:
标签: python pandas ipython-notebook
是否可以在显示 pandas 数据框时隐藏索引,以便仅列名出现在表的顶部?
这需要同时适用于 ipython notebook 中的 html 表示和 to_latex() 函数(我与 nbconvert 一起使用)。
Ta。
【问题讨论】:
标签: python pandas ipython-notebook
正如@waitingkuo 所指出的, index=False 是您所需要的。如果你想在你的 ipython notebook 中保留漂亮的表格布局,你可以使用:
from IPython.display import display, HTML
display(HTML(df.to_html(index=False)))
【讨论】:
设置index=False
对于 ipython 笔记本:
print df.to_string(index=False)
对于 to_latex:
df.to_latex(index=False)
【讨论】:
from IPython.display import HTML HTML(df.to_html(index=False))
从 v. 0.17.1 开始,可以通过 styling 隐藏索引,请参阅 hiding the index or colums:如果 df 是您的数据框就可以了
df.style.hide_index()
请注意,样式仅在笔记本中有效,在 LaTeX 转换中无效。
【讨论】:
.style 方法中断了到 Latex/pdf 的转换,仅呈现 <pandas.io.formats.style.Styler at 0x......>。
我将以下单元格添加到我的笔记本中,该单元格在 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>
""")
【讨论】:
设置index=False。
例如:DataFrame.to_csv("filename", index=False)
这会起作用。
【讨论】:
您只需尝试此代码,可能会对您有所帮助。
dataframe.style.hide_index()
【讨论】: