简体   繁体   English

如何计算在列值上传递的行的时间差

[英]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 2021-11-25 15:15:36
Failing失败 2021-11-25 00:46:23 2021-11-25 00:46:23
Failing失败 2021-11-25 00:16:03 2021-11-25 00:16:03
Failing失败 2021-11-24 23:45:08 2021-11-24 23:45:08
Passing通过 2021-11-25 15:15:13 2021-11-25 15:15:13
Failing失败 2021-11-25 00:46:47 2021-11-25 00:46:47
Failing失败 2021-11-25 00:16:09 2021-11-25 00:16:09
Failing失败 2021-11-24 23:44:59 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 2021-11-25 15:15:36 15:30:38 15:30:38 31:00:52 31:00:52
Failing失败 2021-11-25 00:46:23 2021-11-25 00:46:23 15:30:38 15:30:38 31:00:52 31:00:52
Failing失败 2021-11-25 00:16:03 2021-11-25 00:16:03 15:30:38 15:30:38 31:00:52 31:00:52
Failing失败 2021-11-24 23:45:08 2021-11-24 23:45:08 15:30:38 15:30:38 31:00:52 31:00:52
Passing通过 2021-11-25 15:15:13 2021-11-25 15:15:13 15:30:14 15:30:14 31:00:52 31:00:52
Failing失败 2021-11-25 00:46:47 2021-11-25 00:46:47 15:30:14 15:30:14 31:00:52 31:00:52
Failing失败 2021-11-25 00:16:09 2021-11-25 00:16:09 15:30:14 15:30:14 31:00:52 31:00:52
Failing失败 2021-11-24 23:44:59 2021-11-24 23:44:59 15:30:14 15:30:14 31:00:52 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.

相关问题 如何使用panda python迭代时间列中的行以计算时间差 - How to iterate over rows in time column to calculate the time difference using panda python 如何计算列的均值,但仅包括某些行? - How do I calculate mean of a column but only including certain rows? 如何计算 Python 中不同行同一列中的值之间的差异? - How can i calculate the difference between values in different rows same column in Python? 如何使用groupby计算两个连续行之间的时间差? - How to calculate time difference between two succesive rows with groupby? 如何计算相对于分组索引PANDAS的行之间的日期差 - How do I calculate date difference between rows with respect to a grouped index PANDAS 删除列值为 X 且时间差小于 Y 的行 - Drop rows with column value X and time difference less than Y 如何在 pandas DATE 列上创建 pivot 表并计算时间差? - How to create a pivot table on pandas DATE column and calculate time difference? 如何使用具有相同值的列计算不同部分的时差 - How can I calculate time difference for different sections with columns that have the same value 如何计算午夜时列中的值之间的时间差? - how to calculate the time difference between the values in column over midnight? 我如何通过列值唯一标识 csv 中的一行,然后检查另一列是否有“通过”关键字并打印它 - How do i uniquely identify a row in a csv by a column value, and then later check another column for a "Passed" keyword and print it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM