我在一个pandas DataFrame中处理3列('time','SEC','DeviceName')。我使用下面的代码来计算'time'列中的行之间的差异,并分配给'SEC'列:
df['SEC'] = df['time'].diff().dt.total_seconds()
“DeviceName”列可以有几个不同的设备,因此我需要修改它,以便仅在设备名称与前一行匹配时才执行计算,否则将0分配给“SEC”。
例如:
time SEC DeviceName
4/18/2023 2:43:00 Applied_AA-12
4/18/2023 3:13:00 1800 Applied_AA-12 # calculate because the device name matches the previous row
4/18/2023 3:35:53 0 Applied_AA-14 # don't calculate because the device name doesn't match the previous row
4/18/2023 3:36:03 10 Applied_AA-14 # calculate because the device name matches the previous row
1条答案
按热度按时间uyto3xhc1#
可以使用
GroupyBy.diff
:输出: