我想根据下一个单元格中给出的状态,将包含moving
的单元格更改为movingToOpen
或movingToClose
。
有时周期会中断,并且不会从open
移至close
或从close
移至open
这是我当前的数据框:
DateTime moving
id
36 2018-11-12 15:06:02.487 open
37 2018-11-12 15:06:03.691 moving
38 2018-11-12 15:06:05.505 moving
39 2018-11-12 15:06:08.622 close
40 2018-11-12 15:06:09.023 close
41 2018-11-12 15:06:09.931 close
42 2018-11-12 15:06:11.944 moving
43 2018-11-12 15:06:13.756 moving
44 2018-11-12 15:06:15.168 moving
45 2018-11-12 15:06:18.388 close
46 2018-11-12 15:06:20.100 moving
47 2018-11-12 15:06:23.316 moving
48 2018-11-12 15:06:25.730 open
49 2018-11-12 15:06:26.637 open
50 2018-11-12 15:06:27.644 open
51 2018-11-12 15:06:28.550 open
52 2018-11-12 15:06:28.855 open
53 2018-11-12 15:06:29.356 open
54 2018-11-12 15:06:30.563 open
55 2018-11-12 15:06:31.369 moving
56 2018-11-12 15:06:32.575 moving
57 2018-11-12 15:06:35.593 close
我希望它看起来像这样:
DateTime moving
id
36 2018-11-12 15:06:02.487 open
37 2018-11-12 15:06:03.691 movingToClose
38 2018-11-12 15:06:05.505 movingToClose
39 2018-11-12 15:06:08.622 close
40 2018-11-12 15:06:09.023 close
41 2018-11-12 15:06:09.931 close
42 2018-11-12 15:06:11.944 movingToClose
43 2018-11-12 15:06:13.756 movingToClose
44 2018-11-12 15:06:15.168 movingToClose
45 2018-11-12 15:06:18.388 close
46 2018-11-12 15:06:20.100 movingToOpen
47 2018-11-12 15:06:23.316 movingToOpen
48 2018-11-12 15:06:25.730 open
49 2018-11-12 15:06:26.637 open
50 2018-11-12 15:06:27.644 open
51 2018-11-12 15:06:28.550 open
52 2018-11-12 15:06:28.855 open
53 2018-11-12 15:06:29.356 open
54 2018-11-12 15:06:30.563 open
55 2018-11-12 15:06:31.369 movingToClose
56 2018-11-12 15:06:32.575 movingToClose
57 2018-11-12 15:06:35.593 close
我尝试过将其转换为数字并使用np.ediff1d
的方法,但这只能使它发生一行,而且在我看来,这是一种更简单的方法。
答案 0 :(得分:2)
首先用mask
将moving
值转换为NaN
,回填最后一个不丢失的值,并添加到由掩码过滤的原始值:
m = df['moving'] == 'moving'
df.loc[m, 'moving'] = 'movingTo' + df['moving'].mask(m).bfill()
print (df)
DateTime moving
36 2018-11-12 15:06:02.487 open
37 2018-11-12 15:06:03.691 movingToclose
38 2018-11-12 15:06:05.505 movingToclose
39 2018-11-12 15:06:08.622 close
40 2018-11-12 15:06:09.023 close
41 2018-11-12 15:06:09.931 close
42 2018-11-12 15:06:11.944 movingToclose
43 2018-11-12 15:06:13.756 movingToclose
44 2018-11-12 15:06:15.168 movingToclose
45 2018-11-12 15:06:18.388 close
46 2018-11-12 15:06:20.100 movingToopen
47 2018-11-12 15:06:23.316 movingToopen
48 2018-11-12 15:06:25.730 open
49 2018-11-12 15:06:26.637 open
50 2018-11-12 15:06:27.644 open
51 2018-11-12 15:06:28.550 open
52 2018-11-12 15:06:28.855 open
53 2018-11-12 15:06:29.356 open
54 2018-11-12 15:06:30.563 open
55 2018-11-12 15:06:31.369 movingToclose
56 2018-11-12 15:06:32.575 movingToclose
57 2018-11-12 15:06:35.593 close