47

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

J Grif
  • 1,003
  • 2
  • 12
  • 16

8 Answers8

47

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)))
Thomas
  • 1,277
  • 1
  • 12
  • 20
43

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

Please note that styling works only in the notebook, and not within the LaTeX conversion.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Stefano M
  • 4,267
  • 2
  • 27
  • 45
  • 2
    The `.style` method breaks conversion to latex/pdf, rendering only ``. – Martin Thøgersen Feb 29 '20 at 14:51
  • 1
    @MartinThøgersen thx for pointing this out. I was googling for an option to hide the index in the notebook and found this discussion. After some other research I learned about styling and I thought useful to add my answer in this discussion. Unfortunately I didn't pay attention to the OP request for LaTeX also. – Stefano M Feb 29 '20 at 18:29
36

Set index=False

For ipython notebook:

print df.to_string(index=False)

For to_latex:

df.to_latex(index=False)
waitingkuo
  • 89,478
  • 28
  • 112
  • 118
  • This shows the output as plain text. In Jupyter I'm losing the nice table layout. Is there a way to do this while keeping the nice table? – Willem van Doesburg Nov 24 '15 at 09:09
  • 12
    @WillemvanDoesburg: `from IPython.display import HTML HTML(df.to_html(index=False))` – Thomas Feb 29 '16 at 05:52
  • 1
    Not asked in the questions, but I came here for the DataFrame.to_csv function. For future reference, it also accept this. – marcelocra Jan 20 '18 at 12:01
  • I don't see how this fully answers the question. There should be _one_ command that displays the df correctly in the nb (as html), and while exporting to latex/pdf it will render as table. (Both without index of course.) The HTML() method by @WillemvanDoesburg fix the HTML parts but breaks latex. – Martin Thøgersen Mar 01 '20 at 14:04
  • Specifically HTML() returns "" with nbconvert -to pdf. – Martin Thøgersen Mar 01 '20 at 14:15
6

Try

df.style.hide(axis="index")

Else you will see:

FutureWarning: this method is deprecated in favour of `Styler.hide(axis="index")`
  df.loc[df.index,['name','depth']].style.hide_index()

See DEPR REF: hide(axis=..) replaces hide_index and hide_columns #43771

questionto42
  • 7,175
  • 4
  • 57
  • 90
5

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>
""")
3

The style.hide_index is depreciated since Pandas 1.4. For those wanting to know how to hide the index in Notebooks with latest pandas use:

df.style.hide(axis='index')
mrkbutty
  • 489
  • 1
  • 5
  • 13
1

Set index=False.

E.g:

DataFrame.to_csv("filename", index=False)

This will work.

1

You just try this code, might help you.

dataframe.style.hide_index()
pmadhu
  • 3,373
  • 2
  • 11
  • 23