显示pandas数据帧时是否可以隐藏索引,以便只有列名出现在表的顶部?
这需要适用于ipython notebook中的html表示和to_latex()函数(我正在使用nbconvert)。
ta.
设置index=False
对于ipython笔记本:
print df.to_string(index=False)
对于to_latex:
df.to_latex(index=False)
正如@waitingkuo所指出的那样,index = False就是你所需要的。如果你想在你的ipython笔记本中保留漂亮的表格布局,你可以使用:
from IPython.display import display, HTML
display(HTML(df.to_html(index=False)))
从v.1.17.1开始,可以通过styling隐藏索引,请参阅hiding the index or colums:如果df
是您的数据框,那么就做
df.style.hide_index()
我将以下单元格添加到我的笔记本中,这在Jupyter 4.0.2中运行良好。
注意:即使没有索引,它也会删除'any'表的第一列。
# 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)
这会奏效。