gpt4 book ai didi

python - pandas - 根据仅日期提供的 maxtimestamp 在其他列中查找值

转载 作者:行者123 更新时间:2023-12-01 01:30:36 25 4
gpt4 key购买 nike

如何获取任何给定日期的 v2(最大时间戳)。所以假设我只有名为 2017-04-03 的日期,那么如何在日期 2017-04-03 的最大时间戳处找到 v2 (2017-04-03 16:30:00 )。我知道df.loc

                       v1    v2
2017-04-03 09:15:00 35.7 35.4
2017-04-03 16:30:00 82.7 82.6
2017-04-04 09:15:00 24.3 24.2
2017-04-04 16:30:00 70.2 70.6
2017-04-28 09:15:00 31.7 31.4
2017-04-28 16:30:00 33.0 33.7

最佳答案

我们可以使用df.index获取索引并使用以下方法过滤它:

from datetime import date

df.index.date == date(2017, 4, 3)

返回 bool 值列表:

>>> df.index.date == date(2017, 4,3)
array([ True, True, False, False, False, False])

接下来我们可以通过以下方式获取这些索引的最大时间戳:

>>> df.index[df.index.date == date(2017, 4,3)].max()
Timestamp('2017-04-03 16:30:00')

因此我们可以使用df.loc[..]来获取相应的记录:

>>> df.loc[df.index[df.index.date == date(2017, 4,3)].max()]
v1 82.7
v2 82.6
Name: 2017-04-03 16:30:00, dtype: float64

v2 的相应值:

>>> df.loc[df.index[df.index.date == date(2017, 4,3)].max()].v2
82.6

关于python - pandas - 根据仅日期提供的 maxtimestamp 在其他列中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910953/

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