Something along this line:
# we work with timedelta type
df['time'] = pd.to_timedelta(df['time']+':00')
# we want to label the `in` and `out` in pairs
df['no_action'] = df.sort_values('time').groupby(['Name','date','action']).cumcount()
# pivot table so `in`, `out` pairs are on the same row
actions = df.pivot(index=['Name','date', 'no_action'], columns='action', values='time')
# differences between `in` and `out`
actions['diff'] = actions['out'] - actions['in']
# finally sum over `Name` and `date` then pivot
actions['diff'].sum(level=['Name','date']).unstack('date')
Output:
date day1 day2
Name
Adam 0 days 04:00:00 NaT
Bert 0 days 03:21:00 0 days 01:10:00
Chrissy 0 days 09:00:00 0 days 09:00:00
Earl 0 days 05:59:00 0 days 01:12:00
Note: this doesn't count the in
and out
pairs that span over midnight.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…