简体   繁体   English

如何检查熊猫行中的连续差异

[英]how to check consecutive difference in rows in pandas

I have following dataframe in pandas 我在熊猫中有以下数据框

   code     car_no      date         time        error_code
   123      6           2019-01-01   06:23:00    E09
   123      6           2019-01-01   06:25:00    E28
   123      6           2019-01-01   06:28:00    E09
   123      6           2019-01-01   22:00:00    E28
   123      7           2019-01-01   08:23:00    E09
   123      6           2019-01-01   09:23:00    E09
   123      7           2019-01-01   08:28:00    E28

What I want to flag, is for a specific code and car_no on the same date if E09 comes first and E28 come later with less than 2 hours difference then flag should be set. 我要标记的是同一日期的特定代码和car_no,如果E09 comes first and E28 come later with less than 2 hours difference则应该设置标记。 My desired dataframe is as follows 我想要的数据帧如下

  code     car_no      date         time        error_code   flag
   123      6           2019-01-01   06:23:00    E09         1
   123      6           2019-01-01   06:25:00    E28         1 
   123      6           2019-01-01   06:28:00    E09         0
   123      6           2019-01-01   22:00:00    E28         0
   123      7           2019-01-01   08:23:00    E09         1
   123      6           2019-01-01   09:23:00    E09         0 
   123      7           2019-01-01   08:28:00    E28         0

How can I do it in pandas? 我该怎么做在熊猫里?

Writing down your conditions and do it within groupby , then we just need to assign it back 写下您的条件并在groupby ,然后我们只需将其分配回去即可

#df.time=pd.to_timedelta(df.time) 
s=df.groupby(['date','car_no']).\
      apply(lambda x : x.error_code.eq('E28')&x.error_code.shift().eq('E09')&x.time.diff().dt.seconds.lt(60*60*2))
s=(s|s.groupby(level=[0,1]).shift(-1)).reset_index(level=[0,1],drop=True)
df['flag']=s
df
Out[126]: 
   code  car_no        date     time error_code   flag
0   123       6  2019-01-01 06:23:00        E09   True
1   123       6  2019-01-01 06:25:00        E28   True
2   123       6  2019-01-01 06:28:00        E09  False
3   123       6  2019-01-01 22:00:00        E28  False
4   123       7  2019-01-01 08:23:00        E09   True
5   123       6  2019-01-01 09:23:00        E09  False
6   123       7  2019-01-01 08:28:00        E28   True

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

相关问题 如何检查日期在 pandas 中是否有连续行? - How do I check if a date has consecutive rows in pandas? 如何检查 pandas 列中接下来的 3 个连续行是否具有相同的值? - How to check if next 3 consecutive rows in pandas column have same value? 如何检查 5 行的每个值是否连续相同 pandas - How to check each value consecutive same for 5 rows pandas 两个非连续行的差异 - Pandas - Difference of two non-consecutive rows - Pandas 如何计算熊猫连续行的两个不同字段之间的时间差? - How to calculate time difference between two different field of consecutive rows in pandas? Python:Pandas系列 - 连续日期时间行之间的差异,以秒为单位 - Python: Pandas Series - Difference between consecutive datetime rows in seconds Pandas 数据帧中任意两连续行之间差异的平均值 - Pandas average of the difference between any two consecutive rows in dataframe 计算pandas中连续两行之间的时间差 - calculate the time difference between two consecutive rows in pandas 熊猫计算连续行之间存在X秒差异的次数 - Pandas calculate the number of times there is X sec difference between consecutive rows Python pandas 在 dataframe 中有效地发现连续行的时间差 - Python pandas spot the time difference in a dataframe efficiently for consecutive rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM