0

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']
benjimin
  • 4,043
  • 29
  • 48
jomjac
  • 21
  • 3
  • Please check if this helps: [How to convert index of a pandas dataframe into a column](https://stackoverflow.com/a/54276300/7789963). – medium-dimensional Dec 25 '22 at 18:16
  • Date is the index to identify/access the row, You could have another index (eg 0, 1, 2 ..) and remove the Date information but you must have an Index of some kind. – user19077881 Dec 25 '22 at 18:19
  • 1
    **Please** read [this meta post](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) and fix your post title and text. – STerliakov Dec 25 '22 at 18:47

2 Answers2

1

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)
MSS
  • 3,306
  • 1
  • 19
  • 50
0

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
Lodinn
  • 462
  • 2
  • 9