如何计算pandas中每个员工每天的时差?

how to calculate time difference for each day for every employee in pandas?

我有这个数据框

    Matricule         DateTime        Date      Time
1   10  2022-01-06 10:59:51 2022-01-06  10:59:51
2   10  2022-01-07 08:40:09 2022-01-07  08:40:09
3   10  2022-01-26 15:39:10 2022-01-26  15:39:10
4   11  2022-01-03 14:33:38 2022-01-03  14:33:38
81  11  2022-01-04 10:04:18 2022-01-04  10:04:18
... ... ... ... ...
15  18  2022-01-24 15:51:22 2022-01-24  15:51:22
15  18  2022-01-24 15:51:29 2022-01-24  15:51:29
15  18  2022-01-24 16:54:23 2022-01-24  16:54:23
15  18  2022-01-28 14:42:01 2022-01-28  14:42:01
15  18  2022-01-28 14:42:32 2022-01-28  14:42:32

我想为每个员工计算每天的第一次和一天的最后一次之间的时间差,例如他每天工作了多少小时

Matricule          Date  WorkTime      
1   10     2022-01-06  1
2   10     2022-01-07  3
3   10     2022-01-26  5
4   11     2022-01-03  2
81  11     2022-01-04  8

你可以使用split-apply-combine方法,为每个组写一个func并应用到groupby上:

grpd = df.groupby(['Matricule', 'Date'])

def get_hours(df):
    start = df['Time'].min()
    end = df['Time'].max()
    new_df = pd.DataFrame([end-start], columns=['WorkTime'])
    return new_df

grpd.apply(get_hours)