您可以通过查看的数量
event start和的累加总和来实现此目的
event end:
>>> data['event number'] = (data.event == 'event start').cumsum()>>> data event event number2010-01-01 00:20:00 event start 12010-01-01 00:30:00-- 12010-01-01 00:40:00-- 12010-01-01 00:50:00-- 12010-01-01 01:00:00-- 12010-01-01 01:10:00 event end 12010-01-01 01:20:00-- 12010-01-01 02:20:00-- 12010-01-01 02:30:00 event start 22010-01-01 02:40:00-- 22010-01-01 02:50:00-- 22010-01-01 03:00:00-- 22010-01-01 03:10:00-- 22010-01-01 03:20:00-- 22010-01-01 03:30:00 event end 2
现在,您只需要设置
nan为没有事件即可;但这些位置对应于行的累积累加
event start等于的累积累加
event end(移动1行)
>>> idx = data['event number'] == (data.event.shift(1) == 'event end').cumsum()>>> data.loc[idx, 'event number'] = np.nan>>> data event event number2010-01-01 00:20:00 event start 12010-01-01 00:30:00-- 12010-01-01 00:40:00-- 12010-01-01 00:50:00-- 12010-01-01 01:00:00-- 12010-01-01 01:10:00 event end 12010-01-01 01:20:00--NaN2010-01-01 02:20:00--NaN2010-01-01 02:30:00 event start 22010-01-01 02:40:00-- 22010-01-01 02:50:00-- 22010-01-01 03:00:00-- 22010-01-01 03:10:00-- 22010-01-01 03:20:00-- 22010-01-01 03:30:00 event end 2[15 rows x 2 columns]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)