Reputation: 2693
I have a dataframe which uses the date column of each row as a .loc index. See below
>>> aapl.head()
Open High Low Close Volume Adj Close
Date
1980-12-12 28.75 28.88 28.75 28.75 16751200 3.15
1980-12-15 27.38 27.38 27.25 27.25 6281600 2.99
1980-12-16 25.38 25.38 25.25 25.25 3776000 2.77
1980-12-17 25.88 26.00 25.88 25.88 3087200 2.84
1980-12-18 26.62 26.75 26.62 26.62 2623200 2.92
[5 rows x 6 columns]
I am trying to retrieve the date of the first row, however (I assume it's because date is used as the index), the good ol' date=aapl.iloc[0]['Date']
doesn't work...though it does work for any other column.
My question is, specifically, how can I retrieve the date value for the first row? That is to say, in general terms, how do I retrieve the .loc index of the first row of a dataframe?
Thanks in advance; help much appreciated.
Upvotes: 3
Views: 8882
Reputation: 13955
You need to access the index of the data frame, not the columns. To do this you can do the following:
DF.index[x]
So in your case:
appl.index[0]
Incidentally if you want to get 'Date' out of the index, so you can reference it in the way you are accustomed to (i.e. as a column) you can reset the index:
appl.reset_index(inplace = True)
And then if you decide you don't like that you can just change it back:
appl.set_index('Date', inplace = True)
Upvotes: 4