Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

python - Pandas dataset: get time spent

I'm stuck at wrapping my head around a puzzle I'm trying to solve in pandas. Normally I use Excel to fix this in a very manual way, but I wanted to find a pandas way.

What I have is a table containing start and stop times from workers (small excerpt):

Name action date time
Adam in day1 08:00
Bert in day1 08:09
Chrissy in day1 09:00
Bert out day1 11:30
Adam out day1 12:00
Bert in day1 12:00
Chrissy out day1 18:00
Earl in day1 18:00
Earl out day1 23:59
Earl in day2 09:00
Bert in day2 09:01
Chrissy in day2 10:00
Bert out day2 10:11
Earl out day2 10:12
Bert in day2 10:15
Chrissy out day2 19:00
question from:https://stackoverflow.com/questions/66067839/pandas-dataset-get-time-spent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...