derNincompoop
derNincompoop

Reputation: 692

Pandas diff() functionality on two columns in a dataframe

I have a data frame in which column A is the start time of an activity and column B is the finish time of that activity, and each row represents an activity (rows are arranged chronologically). I want to compute the difference in time between the end of one activity and the start of the next activity, i.e. df[i+1][A] - df[i][B].

Is there a Pandas function to do this (the only thing I can find is diff(), but that only appears to work on a single column).

Upvotes: 8

Views: 6433

Answers (1)

Happy001
Happy001

Reputation: 6383

You can shift A column first:

df['A'].shift(-1) - df['B']

Upvotes: 9

Related Questions