仅计数每个序列Python首次出现

发布于 2025-01-30 07:08:31 字数 381 浏览 1 评论 0原文

我有一些加速数据,如果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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅淡淡 2025-02-06 07:08:32

我不熟悉其中的功能。我想我可能会尝试从算法的角度提供帮助。

假设我们有一个列表a = [0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,...,...,0]

从算法POV中您想在此序列的开始时用一个唯一的序列替换为1的唯一序列是您要做的:

  • 解析列表
  • 评估如果是一个,则是一个或零
  • ,然后,以下每个项目必须是a 0直到您实际上零,

您可能想要这样的东西:

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

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 :

  • parse the list
  • assess whether it is a one or a zero
  • if it is a one then, each following item must be a 0 until you actually have a zero

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
橘寄 2025-02-06 07:08:31

您可以通过 系列系列.shift 仅获取'1'的值,因此,通过&的链条条件 for Bitwise and and 上次铸造给整数的true/false to 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)

或测试差异,但是由于可能的首先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

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:

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:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文