I'm working with 3 columns ('time', 'SEC', 'DeviceName') in a pandas DataFrame. I'm using the following code to calculate the differences between rows in the 'time' column and assign to the 'SEC' column:
df['SEC'] = df['time'].diff().dt.total_seconds()
The 'DeviceName' column can have several different devices, so I need to modify this to only perform the calculation if the device name matches the previous row, otherwise assign a 0 to 'SEC'.
For example:
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