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
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']
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)
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
Help please, I need to delete the ‘date’ index column, or else ‘date’ will appear in the first column with the actions
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']
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)
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