仅计数每个序列Python首次出现
我有一些加速数据,如果AccelPos列中的加速值> = 2.5使用以下代码
frame["new3"] = np.where((frame.accelpos >=2.5), '1', '0')
i最终以序列的方式获取数据,以便
0,0,0,0,1,1,1,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0
我想将第二列添加到以下数据在每个序列的开始时给出一个1,如下
0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0
所示
I have some acceleration data that I have set up a new column to give a 1 if the accel value in the accelpos column >=2.5 using the following code
frame["new3"] = np.where((frame.accelpos >=2.5), '1', '0')
I end up getting data in sequences like so
0,0,0,0,1,1,1,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0
I want to add a second column to give a 1 just at the start of each sequence as follows
0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0
Any help would be much apreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉其中的功能。我想我可能会尝试从算法的角度提供帮助。
假设我们有一个列表
a = [0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,...,...,0]
从算法POV中您想在此序列的开始时用一个唯一的序列替换为1的唯一序列是您要做的:
您可能想要这样的东西:
I am not familiar with the where function. I guess i might try and help from an algorithmic point of view.
Assume we have a list
a = [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, ..., 0]
From an algorithmic POV if you want to replace each sequence of 1 with a unique one at the begining of such sequence here is what you want to do :
You might want to have something like this :
您可以通过
系列系列.shift
仅获取'1'
的值,因此,通过&
的链条条件 for Bitwise和
and and 上次铸造给整数的true/false
to1/0
映射:或测试差异,但是由于可能的首先
1
是必要的使用fillna
:原始You can compare shifted values by
Series.shift
and get values only for'1'
, so chain conditions by&
for bitwiseAND
and last casting to integers forTrue/False
to1/0
mapping:Or test difference, but because possible first
1
is necessary replace missing value by original withfillna
: