Skip to content
Advertisement

Pandas dataframe diff between rows with column offset

I have a Dataframe with the following structure

time_start time_end label
time time + 1 action
time + 1 time + 2 some_other_action

I would like to take see the diff of time_start and previous row time_end. in this case (time + 1) - (time + 1) = 0 I have tried df.diff, but that only yields the diff within either columns or rows.

Any ideas of how I can perform this “jagged diff”?

Advertisement

Answer

The solution was what @Quang Hoang mentioned in a comment.

df['time_start'] - df['time_end'].shift(periods=1)

Note that periods can be negative to shift in the other direction.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.shift.html

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement