我有一个数据:
<表类>
ID
关闭组message_time tbody><<tr>3 22:00 7:00 19:053 22:00 7:30 19:40 3 22:00 20:00 21:00 bbb 23点22:00 22:10 ccc 22:00 晚7:00 19:05ccc 22:00 7:30 19:40 表类>
将to_timedelta
和fillna
转换为每组的最后一行,差异为:
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