[英]Count only first occurrence of each sequence python
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我有一些加速度数据,如果 accelpos 列中的加速度值 >=2.5 使用以下代码,我已经设置了一个新列以给出 1
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我想添加第二列,以便在每个序列的开头给出一个 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
Any help would be much apreciated任何帮助将不胜感激
You can compare shifted values by Series.shift
and get values only for '1'
, so chain conditions by &
for bitwise AND
and last casting to integers for True/False
to 1/0
mapping:您可以通过
Series.shift
比较移位值并仅获取'1'
值,因此通过&
链接条件用于按位AND
和最后转换为整数以实现True/False
到1/0
映射:
df = pd.DataFrame({'col':'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'.split(',')})
df['new'] = (df['col'].ne(df['col'].shift()) & df['col'].eq('1')).astype(int)
Or test difference, but because possible first 1
is necessary replace missing value by original with fillna
:或测试差异,但因为可能第一个
1
是必要的,用fillna
替换原来的缺失值:
s = df['col'].astype(int)
df['new'] = s.diff().fillna(s).eq(1).astype(int)
print (df)
col new
0 0 0
1 0 0
2 0 0
3 0 0
4 1 1
5 1 0
6 1 0
7 1 0
8 1 0
9 0 0
10 0 0
11 0 0
12 1 1
13 1 0
14 0 0
15 0 0
16 0 0
17 1 1
18 1 0
19 1 0
20 1 0
21 1 0
22 1 0
23 1 0
24 1 0
25 1 0
26 1 0
27 0 0
28 0 0
29 0 0
30 0 0
I am not familiar with the where function. I guess i might try and help from an algorithmic point of view.我不熟悉 where function。我想我可能会尝试从算法的角度提供帮助。
Assume we have a list a = [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, ..., 0]
假设我们有一个列表
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:从算法的 POV 中,如果你想在这样的序列的开头用一个唯一的序列替换 1 的每个序列,这就是你想要做的:
You might want to have something like this:你可能想要这样的东西:
a = [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1]
for i in range(len(a)-1):
if a[i] == 1 :
for j in range(1,len(a)-i):
if a[i+j] == 1:
a[i+j] = 0
else :
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.