基于源数据帧的临时数据帧的长度更新列值



我有一个数据:

<表类> ID 关闭组message_timetbody><<tr>322:007:0019:05322:007:3019:40322:0020:0021:00bbb23点22:0022:10ccc22:00晚7:0019:05ccc22:007:3019:40

to_timedeltafillna转换为每组的最后一行,差异为:

c = pd.to_timedelta(df['closed']+':00')
s = pd.to_timedelta(df['set']+':00')
df['newtime'] = s.groupby(df['ID']).diff(-1).mul(-1).fillna(c-s)

输出:

ID closed    set message_time         newtime
0  aaa  22:00  19:00        19:05 0 days 00:30:00
1  aaa  22:00  19:30        19:40 0 days 00:30:00
2  aaa  22:00  20:00        21:00 0 days 02:00:00
3  bbb  23:00  22:00        22:10 0 days 01:00:00
4  ccc  22:00  19:00        19:05 0 days 00:30:00
5  ccc  22:00  19:30        19:40 0 days 02:30:00

如果您需要以分钟为单位输出:

df['newtime'] = (s.groupby(df['ID']).diff(-1).mul(-1)
.fillna(c-s)
.dt.total_seconds().div(60)
)

输出:

ID closed    set message_time  newtime
0  aaa  22:00  19:00        19:05     30.0
1  aaa  22:00  19:30        19:40     30.0
2  aaa  22:00  20:00        21:00    120.0
3  bbb  23:00  22:00        22:10     60.0
4  ccc  22:00  19:00        19:05     30.0
5  ccc  22:00  19:30        19:40    150.0

相关内容

  • 没有找到相关文章

最新更新