Skip to content
Advertisement

Can I get the value of a index if there seems to be multiple indexes? [duplicate]

I have this df:

JavaScript

I had to pivot this table so I think ticker & date are indexes(?)

df.columns results in:

JavaScript

I wanted to create a new column for the year(I belive this does the trick – df['year'] = pd.to_datetime(df['date'],format='%Y') ) but it’s not finding the date column because I think it’s some kind of index?

How can I access it?

Advertisement

Answer

Your dataframe likely stores the dates in a DatetimeIndex. To access the year of each row in your dataframe, simply use df.index.year

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement