根据上一个/下一个像元值​​确定像元值 [英] Determining a cell value based on previous/next cell value(s)

查看:66
本文介绍了根据上一个/下一个像元值​​确定像元值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的上一篇文章的后续文章:根据其他单元格值更改多个单元格值

This is a follow-up from a previous post of mine: Change multiple cell values based on other cell value(s)

这是对象的正常循环看起来像:

this is what a normal cycle of an object looks like:

                   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

我需要知道对象朝哪个方向移动。所以我所做的是

I need to know in which direction the object is moving. So what i did was

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)]

我合并为一列,然后发表了上一篇文章。
,然后我使用该代码在显示方向的上方创建数据框。在这种情况下有效。但是当周期中断时,方向会根据打开/关闭期间的中断而改变。

which I merged into one column and then made the previous post. and then i used that code to create dataframe above where the direction is shown. Which works in this case. But when the cycle gets interrupted the direction changes based on an interruption during opening/closing.

                   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 光电池= 1 使对象关闭,然后再次开始打开。

at 43 2018-11-12 15:06:13.756 photocells = 1 this will make the object close and then start to open again.

所以此数据帧应该是:

                   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从<$ c开始$ c> 1 到 0 ,它将始终关闭。但是基于df中的其他列,它可以改变方向。如果在打开期间 safetyedgeopen = 1 ,它将再次关闭。但是,如果在打开期间 traploopext = 1 ,它将继续打开。

So what i need is a way to determine whether the object is opening or closing. If limitswitchclose goes from 1 to 0 it will always be opening and if limitswitchopen goes from 1 to 0 it will always be closing. But based on the other columns in df it can change direction. If safetyedgeopen = 1 during opening it will close again. But if traploopext = 1 during opening it will continue opening.

我该如何解决这个问题?

How do i tackle this problem?

(我将继续尝试解决该问题并将其答案发布,如果可以的话,我可以举更多示例说明我希望数据框如何显示,但该帖子越来越long)

(I'll continue trying to solve it and post my answer if it works, I can give more examples of how I want the dataframe to look but the post was getting long)

推荐答案

如果我了解您的主要要求,那就是如何根据上一行的值分配值。我这样做的方法是将过滤条件简单地移动一行。我不会提供所有选项的解决方案,但是您可以适当地扩展过滤条件。

If I understand what you mainly ask, it's "how do I assign a value based on the value of previous row." The way I Would do this is to simply shift the filtering condition by one row. I won't give the solution for all the options, but you can just expand the filtering condition appropriately.

此行代码根据相邻行在光电管中的值为1进行过滤,如果是这种情况,则将当前行 tomoving替换为

this line of code filters by where the neighbouring row would have the value of 1 in photocells, and if that's the case, then replaces the current rows "tomoving" to "toclosing", or "toclosing", to "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('_','')

这篇关于根据上一个/下一个像元值​​确定像元值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆