获取日期索引的Pandas DataFrame中最后一条记录的日期
我正在读取一个每日结束价格的CSV文件,并使用日期这一列来给数据框(dataframe)建立索引。我想检查最后一条记录的日期。我找到了索引值的位置,但还没弄明白怎么获取实际的日期。
这个CSV文件的格式是:日期、开盘价、最高价、最低价,等等。
这里的日期格式是2014-07-28。
import pandas as pd
df = pd.read_csv('c:/datafile.csv', index_col='Date')
lastrec = len(df.index) -1
# how to get 'Date' Value for this last record?
df.iloc[lastrec]
# gives me the Open, High, .... columns and values
df.iloc[lastrec].index
# gives me the list of columns
我之前的其他想法都让我遇到了错误。
我正在通过这个项目学习Python和Pandas。
我该如何从最后一条记录中获取索引值(日期)呢?
2 个回答
0
在你的例子中:
df.index[lastrec]
# gives me the date 2014-07-28 as desired.
1
不需要先获取 lastrec
:
df.index[-1]