Add extra column as the cumulative time difference

前端 未结 3 1309
滥情空心
滥情空心 2021-01-19 13:31

How to add an extra column that is the cumulative value of the time differences for each course? For example, the initial table is:

 id_A       course     we         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-19 14:11

    You can chain the diff method with cumsum:

    # convert ts_A to datetime type
    df.ts_A = pd.to_datetime(df.ts_A)
    
    # convert ts_A to seconds, group by id and then use transform to calculate the cumulative difference
    df['cum_delta_sec'] = df.ts_A.astype(int).div(10**9).groupby(df.id_A).transform(lambda x: x.diff().fillna(0).cumsum())
    df
    

提交回复
热议问题