按时间戳列过滤/选择 pandas 数据框的行 [英] filter/select rows of pandas dataframe by timestamp column
本文介绍了按时间戳列过滤/选择 pandas 数据框的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是熊猫的新手.我有包含两列 dt(日期时间戳)和值的数据框.
I am new to pandas. I have dataframe with two columns dt (date-time stamp) and value.
给定两个开始和结束数据时间戳:有没有一种简单的方法可以从原始数据帧创建一个包含两个日期时间戳之间的行的新数据帧?
Given two start and end data-time stamps: is there a easy way to create a new dataframe from original one that contains rows between the two date-time stamp?
dt value
84 7/23/2014 7:00 0.300
85 7/23/2014 7:05 0.300
86 7/23/2014 7:10 0.312
87 7/23/2014 7:15 0.300
88 7/23/2014 7:20 0.300
89 7/23/2014 7:25 0.300
90 7/23/2014 7:30 0.300
91 7/23/2014 7:35 0.300
92 7/23/2014 7:40 0.300
93 7/23/2014 7:45 0.216
94 7/23/2014 7:50 0.204
95 7/23/2014 7:55 0.228
96 7/23/2014 8:00 0.228
97 7/23/2014 8:05 0.228
98 7/23/2014 8:10 0.228
99 7/23/2014 8:15 0.240
100 7/23/2014 8:20 0.228
101 7/23/2014 8:25 0.216
102 7/23/2014 8:30 0.228
103 7/23/2014 8:35 0.324
104 7/23/2014 8:40 0.336
105 7/23/2014 8:45 0.324
106 7/23/2014 8:50 0.324
107 7/23/2014 8:55 0.324
108 7/23/2014 9:00 0.252
109 7/23/2014 9:05 0.252
110 7/23/2014 9:10 0.240
111 7/23/2014 9:15 0.240
112 7/23/2014 9:20 0.252
113 7/23/2014 9:25 0.240
.. ... ...
198 7/23/2014 16:30 0.132
199 7/23/2014 16:35 0.120
200 7/23/2014 16:40 0.180
201 7/23/2014 16:45 0.216
202 7/23/2014 16:50 0.204
203 7/23/2014 16:55 0.192
推荐答案
只要 dt 是 datetime dtype,您就可以使用日期字符串进行过滤,如果不是,则可以这样做:
So long as dt is a datetime dtype already you can filter using date strings, if not then you can convert doing this:
df['dt'] = pd.to_datetime(df['dt'])
然后过滤:
In [115]:
df[(df['dt'] > '2014-07-23 07:30:00') & (df['dt'] < '2014-07-23 09:00:00')]
Out[115]:
dt value
index
91 2014-07-23 07:35:00 0.300
92 2014-07-23 07:40:00 0.300
93 2014-07-23 07:45:00 0.216
94 2014-07-23 07:50:00 0.204
95 2014-07-23 07:55:00 0.228
96 2014-07-23 08:00:00 0.228
97 2014-07-23 08:05:00 0.228
98 2014-07-23 08:10:00 0.228
99 2014-07-23 08:15:00 0.240
100 2014-07-23 08:20:00 0.228
101 2014-07-23 08:25:00 0.216
102 2014-07-23 08:30:00 0.228
103 2014-07-23 08:35:00 0.324
104 2014-07-23 08:40:00 0.336
105 2014-07-23 08:45:00 0.324
106 2014-07-23 08:50:00 0.324
107 2014-07-23 08:55:00 0.324
这篇关于按时间戳列过滤/选择 pandas 数据框的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文