这是我的上一篇文章的后续报道:Change multiple cell values based on other cell value(s)
这是对象正常循环的样子:
DateTime limitswitchopen limitswitchclose safetyedgeclose safetyedgeopen photocells traploopext rectloopext moving comment
0 2018-11-12 15:04:46.861 0 1 0 0 0 0 0 close NaN
1 2018-11-12 15:04:57.149 0 1 0 0 0 0 0 close NaN
2 2018-11-12 15:05:05.046 0 1 0 0 0 0 0 close Normaal
3 2018-11-12 15:05:06.859 0 0 0 0 0 0 0 movingToopen Normaal
4 2018-11-12 15:05:10.080 0 0 0 0 0 0 0 movingToopen Normaal
5 2018-11-12 15:05:11.801 1 0 0 0 0 0 0 open Normaal
6 2018-11-12 15:05:13.409 1 0 0 0 0 0 0 open Normaal
7 2018-11-12 15:05:17.142 1 0 0 0 0 1 0 open Normaal
8 2018-11-12 15:05:18.754 1 0 0 0 0 1 0 open Normaal
9 2018-11-12 15:05:19.055 1 0 0 0 0 0 1 open Normaal
10 2018-11-12 15:05:19.763 1 0 0 0 0 0 1 open Normaal
11 2018-11-12 15:05:20.367 1 0 0 0 0 0 0 open Normaal
12 2018-11-12 15:05:21.575 0 0 0 0 0 0 0 movingToclose Normaal
13 2018-11-12 15:05:23.385 0 0 0 0 0 0 0 movingToclose Normaal
14 2018-11-12 15:05:26.505 0 1 0 0 0 0 0 close Normaal
15 2018-11-12 15:05:26.906 0 1 0 0 0 0 0 close NaN
我需要知道物体朝哪个方向移动。所以我所做的是
df['moving] = df[(df.limitswitchclose == 0) & (df.limitswitchopen == 0)]
df['open'] = df[(dfsamen.limitswitchclose == 0) & (df.limitswitchopen == 1)]
df['close'] = df[(dfsamen.limitswitchclose == 1) & (df.limitswitchopen == 0)]
我将其合并为一列,然后发表了上一篇文章。 然后我使用该代码在显示方向的上方创建数据框。在这种情况下有效。但是,当周期中断时,方向会根据打开/关闭期间的中断而改变。
DateTime limitswitchopen limitswitchclose safetyedgeclose safetyedgeopen photocells traploopext rectloopext moving comment
41 2018-11-12 15:06:09.931 0 1 0 0 0 0 0 close Fotocellopen
42 2018-11-12 15:06:11.944 0 0 0 0 0 0 0 movingToclose Fotocellopen
43 2018-11-12 15:06:13.756 0 0 0 0 1 0 0 movingToclose Fotocellopen
44 2018-11-12 15:06:15.168 0 0 0 0 0 0 0 movingToclose Fotocellopen
45 2018-11-12 15:06:18.388 0 1 0 0 0 0 0 close Fotocellopen
46 2018-11-12 15:06:20.100 0 0 0 0 0 0 0 movingToopen Fotocellopen
47 2018-11-12 15:06:23.316 0 0 0 0 0 0 0 movingToopen Fotocellopen
48 2018-11-12 15:06:25.730 1 0 0 0 0 0 0 open Fotocellopen
49 2018-11-12 15:06:26.637 1 0 0 0 0 0 0 open Fotocellopen
50 2018-11-12 15:06:27.644 1 0 0 0 0 1 0 open Fotocellopen
51 2018-11-12 15:06:28.550 1 0 0 0 0 1 1 open Fotocellopen
52 2018-11-12 15:06:28.855 1 0 0 0 0 0 1 open Fotocellopen
53 2018-11-12 15:06:29.356 1 0 0 0 0 0 0 open Fotocellopen
54 2018-11-12 15:06:30.563 1 0 0 0 0 0 0 open Fotocellopen
55 2018-11-12 15:06:31.369 0 0 0 0 0 0 0 movingToclose Fotocellopen
56 2018-11-12 15:06:32.575 0 0 0 0 0 0 0 movingToclose Fotocellopen
57 2018-11-12 15:06:35.593 0 1 0 0 0 0 0 close Fotocellopen
在43 2018-11-12 15:06:13.756
photocells = 1
处这将使对象关闭,然后再次开始打开。
所以这个数据框应该是:
DateTime limitswitchopen limitswitchclose safetyedgeclose safetyedgeopen photocells traploopext rectloopext moving comment
41 2018-11-12 15:06:09.931 0 1 0 0 0 0 0 close Fotocellopen
42 2018-11-12 15:06:11.944 0 0 0 0 0 0 0 movingToopen Fotocellopen
43 2018-11-12 15:06:13.756 0 0 0 0 1 0 0 movingToopen Fotocellopen
44 2018-11-12 15:06:15.168 0 0 0 0 0 0 0 movingToclose Fotocellopen
45 2018-11-12 15:06:18.388 0 1 0 0 0 0 0 close Fotocellopen
46 2018-11-12 15:06:20.100 0 0 0 0 0 0 0 movingToopen Fotocellopen
47 2018-11-12 15:06:23.316 0 0 0 0 0 0 0 movingToopen Fotocellopen
48 2018-11-12 15:06:25.730 1 0 0 0 0 0 0 open Fotocellopen
49 2018-11-12 15:06:26.637 1 0 0 0 0 0 0 open Fotocellopen
50 2018-11-12 15:06:27.644 1 0 0 0 0 1 0 open Fotocellopen
51 2018-11-12 15:06:28.550 1 0 0 0 0 1 1 open Fotocellopen
52 2018-11-12 15:06:28.855 1 0 0 0 0 0 1 open Fotocellopen
53 2018-11-12 15:06:29.356 1 0 0 0 0 0 0 open Fotocellopen
54 2018-11-12 15:06:30.563 1 0 0 0 0 0 0 open Fotocellopen
55 2018-11-12 15:06:31.369 0 0 0 0 0 0 0 movingToclose Fotocellopen
56 2018-11-12 15:06:32.575 0 0 0 0 0 0 0 movingToclose Fotocellopen
57 2018-11-12 15:06:35.593 0 1 0 0 0 0 0 close Fotocellopen
所以我需要一种确定对象是打开还是关闭的方法。
如果limitswitchclose从1
到0
,它将始终处于打开状态;如果limitswitchopen从1
到0
,它将始终处于关闭状态。但是基于df中的其他列,它可以改变方向。如果safetyedgeopen = 1
在打开过程中将再次关闭。但是如果traploopext = 1
在打开过程中将继续打开。
我该如何解决这个问题?
(我将继续尝试解决它,并在可行的情况下发布我的答案,我可以提供更多示例,说明我希望数据框看起来如何,但是发布时间越来越长)
答案 0 :(得分:1)
如果我了解您的主要要求,那就是“如何根据上一行的值来分配值”。我这样做的方法是将过滤条件简单地移动一行。我不会提供所有选项的解决方案,但是您可以适当地扩展过滤条件。
此行代码根据相邻行在光电管中的值为1进行过滤,如果是这种情况,则将当前行“ tomoving”替换为“ toclosing”或“ toclosing”替换为“ tomoving”
df.loc[df.shift(1).photocells==1,'moving'] = df.loc[df.shift(1).photocells==1,'moving'].str.replace('Toclose','To_open').str.replace('Toopen','To_close').str.replace('_','')