You can use the following functions in the Pandas library to solve this problem:
DataFrame.assign
It assigns new columns to a DataFrame, returning a new object with all the original columns in addition to the new ones.
DataFrame.sort_values
Sort by the values along column or row.
For example:
df.assign(
timediff=df.sort_values(
'datetime', ascending=False
).groupby(['from', 'to']).datetime.diff(-1).dt.seconds.div(60).fillna(0))
Output:
Hope this answer helps.
If you want to be build successful data science career then enroll for best data science certification.