Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe hide index functionality?

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.

like image 537
J Grif Avatar asked Jan 21 '14 10:01

J Grif


People also ask

How do I hide an index in a data frame?

Use hide_index() By using hide_index() method, you can hide the Index of the DataFrame.

Can I remove the index from a Pandas DataFrame?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.

How do I turn off auto index in pandas?

The explanation: Dataframes always have an index, and there is no way of how to remove it, because it is a core part of every dataframe. ( iloc[0:4]['col name'] is a dataframe, too.) You can only hide it in your output.

What does reset_index () do in pandas?

Pandas DataFrame reset_index() Method The reset_index() method allows you reset the index back to the default 0, 1, 2 etc indexes. By default this method will keep the "old" idexes in a column named "index", to avoid this, use the drop parameter.


2 Answers

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))) 
like image 85
Thomas Avatar answered Oct 05 '22 13:10

Thomas


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.

like image 40
Stefano M Avatar answered Oct 05 '22 13:10

Stefano M