Pandas dataframe hide index functionality?

2019-02-06 07:08发布

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.

4条回答
\"骚年 ilove
2楼-- · 2019-02-06 07:41

Set index=False

For ipython notebook:

print df.to_string(index=False)

For to_latex:

df.to_latex(index=False)
查看更多
劫难
3楼-- · 2019-02-06 07:46

Set index=False.

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

This will work.

查看更多
对你真心纯属浪费
4楼-- · 2019-02-06 07:49

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)))
查看更多
孤傲高冷的网名
5楼-- · 2019-02-06 07:50

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>
""")
查看更多
登录 后发表回答