[英]How to use pandas to get data in between certain times within a DataFrame
Here is a small snippet of a dataFrame I am working with:这是我正在使用的 dataFrame 的一小段:
fruit time
0 apple 2021-12-20 17:55:00
1 bannana 2021-12-23 05:13:00
2 apple 2021-12-20 17:55:00
How can I go about getting data in between certain timestamps.我如何 go 关于在某些时间戳之间获取数据。 Such as all data in between 17:00:00 and 18:00:00.
比如17:00:00到18:00:00之间的所有数据。
In addition if possible, I would like to get data in between certain timestamps who's fruit value equals "apple"此外,如果可能的话,我想在某些时间戳之间获取数据,这些时间戳的水果价值等于“苹果”
I have tried df.between_time but I get the error: TypeError: Index must be DatetimeIndex.我试过 df.between_time 但我收到错误:TypeError: Index must be DatetimeIndex。 Seems like the issue is with the timestamp formatting.
似乎问题出在时间戳格式上。
Solution 1: boolean indexing to filter the rows where hour
is between 17
and 18
:解决方案 1: boolean 索引过滤
hour
在17
和18
之间的行:
df[df['fruit'].eq('apple') & df['time'].dt.hour.between(17, 18)]
Solution 2: Set the index to time
column then use between_time
to filter the rows解决方案 2:将索引设置为
time
列,然后使用between_time
过滤行
(
df
.set_index('time')
.query("fruit == 'apple'")
.between_time('17:00:00', '18:00:00')
.reset_index()
)
Result结果
fruit time
0 apple 2021-12-20 17:55:00
2 apple 2021-12-20 17:55:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.