Pandas dataframe diff between rows with column offset

Question:

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

Asked By: Sebastian

||

Answers:

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

Answered By: Sebastian
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.