简体   繁体   English

用前几行的值减去当前行的值并返回较大的值

[英]Subtract value in current row with previous rows values and return the greater value

I would like to subtract the start date in the current row from the end and error date and return the largest timedelta 我想从结束日期和错误日期中减去当前行的开始日期,并返回最大的timedelta

          start                end       error_time
12239   2019-02-18 00:15:13 2019-02-18 01:07:41     NaT
12241   2019-02-18 01:07:56 2019-02-18 01:17:07     NaT
12243   2019-02-18 13:29:51 2019-02-18 13:41:17     NaT
12775   2019-02-18 21:31:27 2019-02-18 23:06:26     NaT
12777   2019-02-18 23:06:57 2019-02-18 23:14:38     NaT
12778   2019-02-19 09:09:51       NaT              2019-02-19 09:10:53
12780   2019-02-19 08:22:57 2019-02-19 23:04:37     NaT
12781   2019-02-19 23:04:37 2019-02-19 23:17:04     NaT
12782   2019-02-20 15:40:11 2019-02-20 15:42:27    2019-03-12 12:00:48

I can already subtract the start date from the previous end date but not sure how to go about comparing this number with the timedelta for start - error and returning the greater of the two values. 我已经可以从上一个结束日期中减去开始日期,但是不确定如何将该数字与timedelta进行比较-错误并返回两个值中的较大者。 I tried using if else statements but that gives me the following error message: 我尝试使用if else语句,但这会给我以下错误消息:

The truth value of a Series is ambiguous. 系列的真实值是不明确的。 Use a.empty, a.bool(), a.item(), a.any() or a.all(). 使用a.empty,a.bool(),a.item(),a.any()或a.all()。

Code I have: 代码我有:

a = br['start'] - br['end'].shift(1)

you can compare two timedelta objects like this: 您可以像这样比较两个timedelta对象:

import datetime

a = datetime(2019,3,3,12,12,12) #start time 1
b = datetime(2020,3,3,12,12,12) #end time 1
c = datetime(2019,3,4,12,12,12) #start time 2
d = datetime(2020,3,2,12,12,12) #end time 2
delta1 = a - b
delta2 = c - d
print(delta1 > delta2) # print False
print(delta1 < delta2) # print True

you dont need to use any special method. 您不需要使用任何特殊方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算前行中大于当前行值的值 - Count values in previous rows that are greater than current row value 检查所有前 n 行中的值是否大于当前行的值 - Check if values in all n previous rows are greater than the value of current row 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column 将当前行值与前一行值进行比较 - Compare current row value to previous row values Pandas:dataframe中的前一个值减去当前值 - Pandas:subtract current value with previous value in dataframe 如何根据 pandas DataFrame 中的条件从当前行值中减去前一行值? - how to subtract previous row value from current row value based on condition in pandas DataFrame? Pandas - 获取当前行,将值与X前一行进行比较并返回多少匹配(在x%范围内) - Pandas - Take current row, compare value to X previous rows and return how many matches (within x% range) 选择当前行中列值为 1 但上一行中值为 0 的行 - Selecting rows where column value is 1 in the current row, but 0 in the previous row 有没有办法根据现有列的当前和前一行值在每行的新列中分配增量值的数量? - Is there a way to assign number of incremental value in a new column of each rows based current and previous row values of existing columns? 将当前行值与所有 6 个先前行值进行比较 - Compare current row value to all 6 previous row values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM