【问题标题】:Get Date of last record in Date indexed pandas dataframe在日期索引的熊猫数据框中获取最后一条记录的日期
【发布时间】:2014-09-21 16:21:40
【问题描述】:

我正在阅读一个日终价格 csv 文件并使用日期列来索引数据框。我想检查最后一条记录的日期。我得到了索引值位置,但还没有弄清楚如何获取实际日期。

CSV 文件的格式为 Date、Open、High、Low、....

这里的日期格式为 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。

如何从最后一条记录中获取索引值(DATE)?

【问题讨论】:

  • 您应该发布您的编辑作为答案,然后接受它

标签: python pandas indexing


【解决方案1】:

Pandas, How to reference Timeseries Items?

所以在你的例子中:

df.index[lastrec]
# gives me the date 2014-07-28 as desired.

【讨论】:

    【解决方案2】:

    无需先获得lastrec

    df.index[-1]
    

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 2021-03-19
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      相关资源
      最近更新 更多