添加额外的列作为累积时间差

编程入门 行业动态 更新时间:2024-10-06 20:33:17
本文介绍了添加额外的列作为累积时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何添加一个额外的列,该列是每个课程的时差的累积值?例如,初始表是:

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 weight ts_A value id1 cotton 3.5 2017-04-27 01:35:30 150.000000 id1 cotton 3.5 2017-04-27 01:36:00 416.666667 id1 cotton 3.5 2017-04-27 01:36:30 700.000000 id1 cotton 3.5 2017-04-27 01:37:00 950.000000 id2 cotton blue 5.0 2017-04-27 02:35:30 150.000000 id2 cotton blue 5.0 2017-04-27 02:36:00 450.000000 id2 cotton blue 5.0 2017-04-27 02:36:30 520.666667 id2 cotton blue 5.0 2017-04-27 02:37:00 610.000000

预期结果是:

id_A course weight ts_A value cum_delta_sec id1 cotton 3.5 2017-04-27 01:35:30 150.000000 0 id1 cotton 3.5 2017-04-27 01:36:00 416.666667 30 id1 cotton 3.5 2017-04-27 01:36:30 700.000000 60 id1 cotton 3.5 2017-04-27 01:37:00 950.000000 90 id2 cotton blue 5.0 2017-04-27 02:35:30 150.000000 0 id2 cotton blue 5.0 2017-04-27 02:36:00 450.000000 30 id2 cotton blue 5.0 2017-04-27 02:36:30 520.666667 60 id2 cotton blue 5.0 2017-04-27 02:37:00 610.000000 90

推荐答案

您可以将diff方法与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

更多推荐

添加额外的列作为累积时间差

本文发布于:2023-10-23 02:34:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间差

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!