Pandas dataframe diff between rows with column offset

Tags: , ,



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”?

Answer

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

df['time_start'] - df['time_end'].shift()



Source: stackoverflow