[英]How to get datetime from a pandas dataframe by row index
如何通過行索引從以下 dataframe 獲取日期? 我正在尋找類似的 function :
get_datetime(dataframe, row_index = 1)
它將返回日期時間為2022-01-11
我已經閱讀了Pandas 的時間序列/日期功能,但是在那里沒有看到任何有用的東西。
open high low close volume dividends stock splits
Date
2022-01-06 21.99 22.22 21.59 22.18 1956300 0 0
2022-01-11 22.32 22.93 22.23 22.92 1885700 0 0
2022-01-21 22.40 22.55 22.02 22.13 2265500 0 0
2022-01-26 22.28 22.72 21.79 22.05 2247600 0 0
2022-01-31 22.02 22.92 21.72 22.86 2955500 0 0
2022-02-10 24.73 25.51 24.59 25.29 4069300 0 0
2022-02-15 24.85 25.57 24.56 25.47 5661100 0 0
2022-02-25 24.59 25.07 24.46 24.67 3722400 0 0
2022-03-02 23.20 23.95 23.15 23.82 3268100 0 0
2022-03-07 21.54 21.60 19.51 19.59 10102600 0 0
您可以使用df.index
訪問索引
df.index[1]
如果您的索引是日期時間,您可以使用DatetimeIndex.date
來獲取datetime.date
對象
df.index.date[1]
或者用DatetimeIndex.strftime
獲取字符串格式
df.index.strftime('%Y-%m-%d')[1]
# use `str` for convenience if the format is %Y-%m-%d
str(df.index.date[1])
做這樣的事情來得到它就像沒有時間戳的日期:
df.index[0].date()
打印: 2022-01-06
您可以使用df.index
來引用行索引。 這是一個類似於列表的 RangeIndex,試試這個,看看會發生什么:
list(df.index)
您應該看到行索引列表。 因此,要引用列表中的任何元素,請使用list[index]
格式,例如
df.index[0]
df.index[-1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.