我有一个数据框,如下所示:
data={'time':['2021-01-01 22:00:12','2021-01-05 22:49:12','2021-01-06 21:00:00','2021-01-06 23:59:15','2021-01-07 05:00:55','2021-01-07 12:00:39'],
'flag':['On','Off','On','Off','On','Off']}
df=pd.DataFrame(data)
我想要获得连续行之间的差异,这是我使用以下命令实现的:
df['diff']=pd.to_datetime(df['time'])-pd.to_datetime(df['time'].shift(1))
But there is calculation overhead here as there is no meaning for difference for every consecutive rows, I only want the difference whenever the flag goes to Off. Also, how to convert the difference into hours ?