Pandas dataframe hide index functionality?
Asked Answered
W

8

52

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

Woods answered 21/1, 2014 at 10:52 Comment(0)
K
36

Set index=False

For ipython notebook:

print df.to_string(index=False)

For to_latex:

df.to_latex(index=False)
Keese answered 21/1, 2014 at 11:2 Comment(5)
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?Judyjudye
@WillemvanDoesburg: from IPython.display import HTML HTML(df.to_html(index=False))Briannebriano
Not asked in the questions, but I came here for the DataFrame.to_csv function. For future reference, it also accept this.Gastronome
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.Cattier
Specifically HTML() returns "<IPython.core.display.HTML object>" with nbconvert -to pdf.Cattier
B
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)))
Briannebriano answered 29/2, 2016 at 6:12 Comment(2)
This only works for HTML, the OP requests is to work with both HTML and Latex.Cattier
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?Terrorize
M
48

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.

Myxomycete answered 6/3, 2019 at 21:15 Comment(3)
The .style method breaks conversion to latex/pdf, rendering only <pandas.io.formats.style.Styler at 0x......>.Cattier
@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.Myxomycete
Edited the answer to update the API to modern Pandas versions.Borg
K
36

Set index=False

For ipython notebook:

print df.to_string(index=False)

For to_latex:

df.to_latex(index=False)
Keese answered 21/1, 2014 at 11:2 Comment(5)
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?Judyjudye
@WillemvanDoesburg: from IPython.display import HTML HTML(df.to_html(index=False))Briannebriano
Not asked in the questions, but I came here for the DataFrame.to_csv function. For future reference, it also accept this.Gastronome
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.Cattier
Specifically HTML() returns "<IPython.core.display.HTML object>" with nbconvert -to pdf.Cattier
H
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

Harwood answered 5/1, 2023 at 18:34 Comment(0)
J
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>
""")
Judyjudye answered 24/11, 2015 at 10:1 Comment(0)
F
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')
Florenceflorencia answered 28/10, 2022 at 8:4 Comment(0)
C
1

Set index=False.

E.g:

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

This will work.

Chlori answered 15/3, 2018 at 18:11 Comment(0)
W
1

You just try this code, might help you.

dataframe.style.hide_index()
Whereunto answered 1/9, 2021 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.