4

I am trying to remove index while converting pandas data-frame into html table. Prototype is as follows:

import pandas as pd
import numpy as np
df= pd.DataFrame({'list':np.random.rand(100)})
html_table = df.to_html()

In html table I don't want to display index.

arshpreet
  • 679
  • 2
  • 11
  • 27

2 Answers2

9

Try this:

html_table = df.to_html(index = False)
Wei
  • 133
  • 1
  • 4
4

It seems you need remove index name:

df = df.rename_axis(None)

Or:

df.index.name = None

For not display index use:

print (df.to_string(index=False))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Yes that removes the index name but not 'index column' as I found(from Stack-overflow ) that it is not possible to remove index from Dataframe but column can be used as index but I need the column that can be used as index in the same level of other columns. – arshpreet Oct 11 '17 at 10:30
  • @arshpreet - I really dont understand - what is desired output? Do you need index name and only remove empty row? I think it is not possible. – jezrael Oct 11 '17 at 10:37
  • But you can use `df.columns.name='a'` and `df.index.name = None` - it looks nice, but after `df = df.reset_index()` get column called `index` – jezrael Oct 11 '17 at 10:43
  • that means we simple can't remove index column from Pandas DataFrame and also can't make 'index column name' in same row as oher columns exist? – arshpreet Oct 11 '17 at 10:52
  • 1
    You need always some index in pandas dataframe. You cannot remove it, only reaasign to default `df.reset_index(drop=True)` or set to another column. – jezrael Oct 11 '17 at 12:26
  • yes that is fine, I was aware of that but isn't it some kind of limit, I mean yes AFAIK index needs to be there for Pandas to keep track of columns but there should be one options either one wants to display it or not. – arshpreet Oct 11 '17 at 13:24
  • Then need `df.to_string(index=False)` ;) – jezrael Oct 11 '17 at 13:25
  • @arshpreet - Done. – jezrael Oct 12 '17 at 06:13