Pandas dataframe hide index functionality?

浪子不回头ぞ 提交于 2019-11-26 16:33:56

问题


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).

Ta.


回答1:


Set index=False

For ipython notebook:

print df.to_string(index=False)

For to_latex:

df.to_latex(index=False)



回答2:


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:

from IPython.display import display, HTML
display(HTML(df.to_html(index=False)))



回答3:


Starting from v. 0.17.1 it is possible to hide the index via styling, see hiding the index or colums: if df is your Data Frame just do

df.style.hide_index()



回答4:


I added the following cell to my notebook which works fine in 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>
""")



回答5:


Set index=False.

E.g: DataFrame.to_csv("filename", index=False)

This will work.



来源:https://stackoverflow.com/questions/21256013/pandas-dataframe-hide-index-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!