简体   繁体   English

检索pandas数据帧中的.loc索引值

[英]Retrieving .loc index values in pandas dataframe

I have a dataframe which uses the date column of each row as a .loc index. 我有一个数据框,它使用每行的日期列作为.loc索引。 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. 我试图检索第一行的日期,但是(我假设它是因为日期被用作索引),好的' date=aapl.iloc[0]['Date']不起作用...虽然它适用于任何其他列。

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? 也就是说,一般而言,如何检索数据帧第一行的.loc索引?

Thanks in advance; 提前致谢; help much appreciated. 非常感谢。

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 (ie as a column) you can reset the index: 顺便提一下,如果你想从索引中获取'Date',那么你可以按照你习惯的方式(即作为一列)引用它,你可以重置索引:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM