简体   繁体   中英

How to slice a pandas dataframe with multiple timeframes?

I have two variables "start" and "end", which consist of multiple timeframes and I want to use them to slice a dataframe. How can I achieve this with python?

(I only found solutions with two timeframes or solutions where the timeframes had to be put in as strings and not as variable)

variable start:

8    2019-10-06 00:10:00
9    2019-10-06 12:00:00
10   2019-11-06 17:28:00
14   2019-06-15 16:37:00
18   2019-06-19 18:23:00
19   2019-06-20 16:56:00
20   2019-06-21 19:13:00
Name: formation_start_mean, dtype: datetime64[ns]

variable end:

8    2019-10-06 11:59:00
9    2019-11-06 01:00:00
10   2019-12-06 06:33:00
14   2019-06-15 21:06:00
18   2019-06-20 11:59:00
19   2019-06-20 19:01:00
20   2019-06-22 11:59:00
Name: formation_end_mean, dtype: datetime64[ns]

I want to slice the datafram like this:

df.var1[start:end]

you can use loc to index your data df30m.loc[(df30m.Timestamp <= d0) & (df30m.Timestamp >= d1)]

You can set the index to the Timestamp column and then index as well

df.set_index('Timestamp', inplace=True) df[d1:d0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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