简体   繁体   English

熊猫:按时间选择行

[英]Pandas: Selecting rows by time

I have a dataframe with the following structure: 我有一个具有以下结构的数据框:

Date       | Hour     | Duration 
2014-05-12 | 20:05:20 | 2.20
2014-05-12 | 20:06:20 | 4.20
2014-05-12 | 22:05:22 | 3.10
2014-05-12 | 08:05:20 | 7.10

How can I create another dataframe that contains only rows where the values in the time column are between 20:00:00 and 6:59:59? 如何创建另一个仅包含时间列中的值在20:00:00和6:59:59之间的行的数据框?

We can using between 我们可以between

m=pd.to_timedelta(df.Hour).between('06:59:59','20:00:00')
df1=df[~m].copy()

There's between_time if you're willing to make a DatetimeIndex . between_time如果你愿意做一个DatetimeIndex Not too different from between , but it deals with the 24 hour clock appropriately. 两者between没有太大差异,但是它可以适当地处理24小时制。

df.index = pd.to_datetime(df.Date + ' ' + df.Hour)
df.between_time('20:00:00', '6:59:59')

#                           Date      Hour  Duration
#2014-05-12 20:05:20  2014-05-12  20:05:20       2.2
#2014-05-12 20:06:20  2014-05-12  20:06:20       4.2
#2014-05-12 22:05:22  2014-05-12  22:05:22       3.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM