Source From Here
Question
I have a pandas dataframe as follows
The above dataframe has 83000 rows. I want to take time difference between two consecutive rows and keep it in a separate column. The desired result would be
I have tried
df['Time_diff'] = df['Time'].diff(-1) but getting error as shown below
How to solve this?
How-To
Problem is pandas need datetimes or timedeltas for diff function, so first converting by to_timedelta, then get total_seconds and divide by 60:
If want
floor or round per minutes:
Question
I have a pandas dataframe as follows
- Dev_id Time
- 88345 13:40:31
- 87556 13:20:33
- 88955 13:05:00
- ..... ........
- 85678 12:15:28
- Dev_id Time Time_diff(in min)
- 88345 13:40:31 20
- 87556 13:20:33 15
- 88955 13:05:00 15
How to solve this?
How-To
Problem is pandas need datetimes or timedeltas for diff function, so first converting by to_timedelta, then get total_seconds and divide by 60:
- >>> import pandas as pd
- >>> from datetime import datetime, timedelta
- >>> df = pd.DataFrame([[1, datetime.now()], [2, datetime.now()- timedelta(hours = 1)]], columns = ['id', 'time'])
- >>> df
- id time
- 0 1 2019-01-24 09:10:19.732798
- 1 2 2019-01-24 08:10:19.732864
- >>> df['time_diff'] = df['time'].diff(-1).dt.total_seconds().div(60)
- >>> df
- id time time_diff
- 0 1 2019-01-24 09:10:19.732798 59.999999
- 1 2 2019-01-24 08:10:19.732864 NaN
- >>> df['time'].diff(-1).dt
沒有留言:
張貼留言