How to hide the index column of a pandas dataframe?

Question:

Help please, I need to delete the ‘date’ index column, or else ‘date’ will appear in the first column with the actions

enter image description here


heat_ds = pd.DataFrame(columns=['PFE','GS','BA','NKE','V','AAPL','TSLA','NVDA','MRK','CVX','UNH'])

heat_ds['PFE'] = pfizer['Close']

heat_ds['GS'] = goldmans['Close']

heat_ds['BA'] = boeingc['Close']

heat_ds['NKE'] = nike['Close']

heat_ds['V'] = visa['Close']

heat_ds['AAPL'] = aaple['Close']

heat_ds['TSLA'] = tesla['Close']

heat_ds['NVDA'] = tesla['Close']

heat_ds['MRK'] = tesla['Close']

heat_ds['CVX'] = chevronc['Close']

heat_ds['UNH'] = unitedh['Close']
Asked By: jomjac

||

Answers:

First of all date represents index. To drop it first reset index to remove date from index of dataframe and make it a normal column and then drop that column.

heat_ds = heat_ds.reset_index()
heat_ds = heat_ds.drop('index', axis=1)

or in one line

 heat_ds = heat_ds.reset_index(drop=True)
Answered By: MSS

Deleting the index is probably not the best approach here.

If you are concerned about display, Styler.hide_index() or Styler.hide() (depending on your version of Pandas) would work. Usage examples here.

For my older version of Pandas,

df.style.hide_index()

in Jupyter cell works just fine. Of course, for exporting to csv, you would use index=False if needed.

If you wish to still print the index, but hide the extra offset caused by the index name, you can set the latter to None:

df.index.name = None
Answered By: Lodinn
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.