Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

pandas - How to take rows with continous time for more than 3 three rows python

I have one data frame i want to get rows when time is continuous for three rows and delete other rows.

df_input:

Value         time 
8970    2020-11-20 15:40:00
7602    2020-11-20 15:50:00
7603    2020-11-20 16:00:00
7604    2020-11-20 16:10:00
7757    2020-11-29 06:30:00
7758    2020-11-29 06:40:00
7877    2020-12-02 01:00:00
11179   2021-01-06 23:50:00
11230   2021-01-07 08:20:00
11283   2021-01-07 17:30:00 

df_out:

8970    2020-11-20 15:40:00
7602    2020-11-20 15:50:00
7603    2020-11-20 16:00:00
question from:https://stackoverflow.com/questions/65951706/how-to-take-rows-with-continous-time-for-more-than-3-three-rows-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

by

df['timeDiff'] = df['time'].diff()

you will get

df_input 
Value         time 
8970    2020-11-20 15:40:00
7602    2020-11-20 15:50:00
7603    2020-11-20 16:00:00
7604    2020-11-20 16:10:00
7757    2020-11-20 18:30:00
7758    2020-11-20 20:30:00

into :

df_output 
Value         timeDiff 
8970    Na
7602    00:10:00
7603    00:10:00
7604    00:10:00
7757    02:20:00
7758    02:00:00

then you can group them as you need to determine continues. I guess this will help you to solve the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...