54

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

8 Answers 8

51

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(axis="index")
# Legacy Pandas versions
df.style.hide_index()

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

Sign up to request clarification or add additional context in comments.

3 Comments

The .style method breaks conversion to latex/pdf, rendering only <pandas.io.formats.style.Styler at 0x......>.
@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.
Edited the answer to update the API to modern Pandas versions.
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)))

2 Comments

This only works for HTML, the OP requests is to work with both HTML and Latex.
Is this efficient if you render a notebook with 20 different df's to html? You have to insert this line for each data-frame?
36

Set index=False

For ipython notebook:

print df.to_string(index=False)

For to_latex:

df.to_latex(index=False)

5 Comments

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?
@WillemvanDoesburg: from IPython.display import HTML HTML(df.to_html(index=False))
Not asked in the questions, but I came here for the DataFrame.to_csv function. For future reference, it also accept this.
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.
Specifically HTML() returns "<IPython.core.display.HTML object>" with nbconvert -to pdf.
8

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

Comments

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

Comments

4

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

Comments

1

You just try this code, might help you.

dataframe.style.hide_index()

1 Comment

This function has been deprecated, use following instead: dataframe.style.hide(axis='index')
0

Set index=False.

E.g:

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

This will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.