I have the dataset,
ID Date
1 12/12/2020 13:00
1 12/12/2020 14:00
1 12/12/2020 15:00
1 12/12/2020 16:00
2 12/13/2020 13:00
2 12/13/2020 13:15
2 12/13/2020 14:00
2 12/13/2020 14:30
Expected output,
ID TimeDelta
1 120mins
2 45mins
I need to find time difference between (row 1 and row 2), (row 3 and row 4) and so on for each GROUPBY and then ADD the difference
One way is to enumerate the rows, then pivot:
enum = df.groupby('ID').cumcount()
df['col'] = enum % 2
df['row'] = enum //2
tmp = df.pivot_table(index=['ID','row'], columns='col',
values='Date', aggfunc='first')
tmp[1].sub(tmp[0]).sum(level=0)
Output:
ID
1 0 days 02:00:00
2 0 days 00:45:00
dtype: timedelta64[ns]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.