gpt4 book ai didi

python-3.x - Pandas:如何获取数据帧第一行和最后一行的键(索引)

转载 作者:行者123 更新时间:2023-12-02 20:23:25 27 4
gpt4 key购买 nike

我有一个带有日期时间索引和一个字段“myfield”的数据框 (df)

我想找出数据帧的第一个和最后一个日期时间。

我可以像这样访问第一个和最后一个数据框元素:

df.iloc[0]
df.iloc[-1]

对于 df.iloc[0] 我得到结果:

myfield myfieldcontent

Name: 2017-07-24 00:00:00, dtype: float

如何访问该行的日期时间?

最佳答案

您可以使用 [0][-1] 选择index:

df = pd.DataFrame({'myfield':[1,4,5]}, index=pd.date_range('2015-01-01', periods=3))
print (df)
myfield
2015-01-01 1
2015-01-02 4
2015-01-03 5

print (df.iloc[-1])
myfield 5
Name: 2015-01-03 00:00:00, dtype: int64

print (df.index[0])
2015-01-01 00:00:00

print (df.index[-1])
2015-01-03 00:00:00

关于python-3.x - Pandas:如何获取数据帧第一行和最后一行的键(索引),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45320659/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com