简体   繁体   English

如何使用 pandas 获取 DataFrame 中特定时间之间的数据

[英]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 索引过滤hour1718之间的行:

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.

相关问题 如何获取 Pandas 列中的某些数据? - How to Get Certain Data Within a Pandas Column? 如何在两次之间获取熊猫数据框列中的值? - How to get values in a pandas dataframe column between 2 times? 如何在熊猫数据框中两次隔离,然后仅在较大的熊猫数据框中修改这些值? - How isolate between two times in pandas data frame and then modify just those values in a larger pandas dataframe? 获取两个输入时间之间的随机时间数据框python python - Get dataframe of random time between two input times pandas python 如何在pandas DataFrame中将某些值替换为NaN时避免转换数据类型? - How to avoid convertion of data type when substituting certain values to NaN within a pandas DataFrame? 如何在时间之间过滤 Pandas 中的数据? - How to filter data in Pandas between times? 如何在熊猫数据框中使用groupby获取以下数据的均值? - How to use groupby in pandas dataframe to get mean for the following data? 如何在 pandas DataFrame 列中获取 datetime.times 之间的差异 - How to take difference between datetime.times in a pandas DataFrame column 如何在Django中格式化pandas数据框以与pandas-highcharts一起使用 - How to format a pandas dataframe for use with pandas-highcharts within Django Pandas 累积和,如果在某些时间/值之间 - Pandas cumulative sum if between certain times/values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM