[英]How do I calculate time difference of rows passed on column value
I have a pandas dataframe like我有一个 pandas dataframe 之类的
Status![]() |
Time Stamp![]() |
---|---|
Passing![]() |
2021-11-25 15:15:36 ![]() |
Failing![]() |
2021-11-25 00:46:23 ![]() |
Failing![]() |
2021-11-25 00:16:03 ![]() |
Failing![]() |
2021-11-24 23:45:08 ![]() |
Passing![]() |
2021-11-25 15:15:13 ![]() |
Failing![]() |
2021-11-25 00:46:47 ![]() |
Failing![]() |
2021-11-25 00:16:09 ![]() |
Failing![]() |
2021-11-24 23:44:59 ![]() |
I need to get the time of the first passing event to the first instance of when it failed for that sequence.我需要将第一个传递事件的时间获取到该序列失败的第一个实例。 So the difference
row 0
and row 3
, and add it to a new column.所以区别
row 0
row 3
,并将其添加到新列中。
Then I need it to calculate the next sequence and add it to the value in the new column.然后我需要它来计算下一个序列并将其添加到新列中的值中。
So the difference between row 4
and row 7
and add the difference to the previous time so I get the total time it was failing.因此,
row 4
row 7
行之间的差异并将差异添加到上一次,这样我就得到了失败的总时间。
This is what the df should look like at the end这就是 df 最后的样子
Status![]() |
Time Stamp![]() |
Downtime![]() |
Total Downtime![]() |
---|---|---|---|
Passing![]() |
2021-11-25 15:15:36 ![]() |
15:30:38 ![]() |
31:00:52 ![]() |
Failing![]() |
2021-11-25 00:46:23 ![]() |
15:30:38 ![]() |
31:00:52 ![]() |
Failing![]() |
2021-11-25 00:16:03 ![]() |
15:30:38 ![]() |
31:00:52 ![]() |
Failing![]() |
2021-11-24 23:45:08 ![]() |
15:30:38 ![]() |
31:00:52 ![]() |
Passing![]() |
2021-11-25 15:15:13 ![]() |
15:30:14 ![]() |
31:00:52 ![]() |
Failing![]() |
2021-11-25 00:46:47 ![]() |
15:30:14 ![]() |
31:00:52 ![]() |
Failing![]() |
2021-11-25 00:16:09 ![]() |
15:30:14 ![]() |
31:00:52 ![]() |
Failing![]() |
2021-11-24 23:44:59 ![]() |
15:30:14 ![]() |
31:00:52 ![]() |
Note that this is example data and the index's of passing and failing events are at different index each time.请注意,这是示例数据,通过和失败事件的索引每次都在不同的索引处。
Here is my code这是我的代码
import pandas as pd
data = {'Status': ['Passing','Failing','Failing','Failing','Passing','Failing','Failing','Failing'],
'TimeStamp': ['2021-11-25 15:15:36','2021-11-25 00:46:23','2021-11-25 00:16:03','2021-11-24 23:45:08','2021-11-25 15:15:13','2021-11-25 00:46:47','2021-11-25 00:16:09','2021-11-24 23:44:59']}
df = pd.DataFrame(data)
I'm self taught in Python and pandas and have no idea how to achieve what I need.我在 Python 和 pandas 自学,不知道如何实现我的需要。 Any help would be appreciated.
任何帮助,将不胜感激。
You did not specify at which index do you mean to add the resulting difference of passing and failing.您没有指定在哪个索引处添加通过和失败的结果差异。 the
oldestFailing
below include all the differences that you need.下面最
oldestFailing
的失败包括您需要的所有差异。
from datetime import datetime as dt
prevPassIdx = 0
prevOldest = dt.now()
oldestFailing = []
for i in range(1, len(df['TimeStamp'])):
if df['Status'][i] == 'Passing':
prevPassIdx = i
oldestFailing.append(prevOldest)
prevOldest = dt.now()
else:
if dt.strptime(df['TimeStamp'][i],"%Y-%m-%d %H:%M:%S") <prevOldest:
prevOldest = dt.strptime(df['TimeStamp'][i],"%Y-%m-%d %H:%M:%S")
if df['Status'][i] != ('Passing'):
oldestFailing.append(prevOldest)
Here, oldestFailing
output is as follows:这里,
oldestFailing
output如下:
[datetime.datetime(2021, 11, 24, 23, 45, 8),
datetime.datetime(2021, 11, 24, 23, 44, 59)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.