0

enter image description herePlease i am trying to name the index column but I can't. I want to be a able to name it such that I can reference it to view the index values which are dates. i have tried

df3.rename(columns={0:'Date'}, inplace=True) but it's not working.

Please can someone help me out? Thank you.

Dennis
  • 105
  • 1
  • 5

2 Answers2

0

As there is no example data frame that you are on, I am listing an arbitrary example to demonstrate the idea.

import datetime as dt
import pandas as pd

data = {'C1'     : [1, 2],
        'Date'  : [dt.datetime.now().strftime('%Y-%m-%d'),dt.datetime.now().strftime('%Y-%m-%d')]}
df = pd.DataFrame(data)
df.index = df["Date"]
del df["Date"]

print(df.index.name) # this will give you the new index column

print(df) #print the dataframe

enter image description here

S.N
  • 4,910
  • 5
  • 31
  • 51
0

Note that the dataframe index cannot be accessed using df['Date'],

I fyou want rename the index, you can use DataFrame.rename_axis:

df=df.rename_axis(index='Date')

if you want to access it as a column then you have to transform it into a column using:

df=df.reset_index()

then you can use:

df['Date']

otherwise you can access the index by:

df.index
ansev
  • 30,322
  • 5
  • 17
  • 31
  • Thank you very much. df=df.rename_axis(index='Date') worked for me. I so much appreciate.@ansev – Dennis Nov 23 '19 at 21:23