python - Searching a data frame via a range of datetime -


hi wondering able search data frame base on column datetime data, want search rows datetime within value.

  time                   user 1 2017-07-13 18:30:18 .  gary 2 2017-07-13 13:30:23 .  mary 3 2017-07-13 02:30:18 .  tim 4 2017-07-12 12:11:44 .  jim 5 2017-07-12 03:31:54 .  tom 6 2017-07-11 01:21:21 .  john 7 2017-07-11 05:32:12 .  sam 

lets want search rows date lands between 7-13-17 00:00:00 7-13-17 15:00:00. result be:

2 2017-07-13 13:30:23 .  mary 3 2017-07-13 02:30:18 .  tim 

is there way slice data selection exact? realize technically create if/elif statement wondering if there easier method creating multiple if/elif statements accomplish task.

use between boolean indexing:

#if necessary convert datetime df['time'] = pd.to_datetime(df['time'])  df = df[df['time'].between('2017-07-13', '2017-07-13 15:00:00')] print (df)                  time  user 2 2017-07-13 13:30:23  mary 3 2017-07-13 02:30:18   tim 

or dataframe.query:

df = df.query("'2017-07-13' < time < '2017-07-13 15:00:00'") print (df)                  time  user 2 2017-07-13 13:30:23  mary 3 2017-07-13 02:30:18   tim 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -