python - 创建一个计数器,迭代数据帧中的列,并在满足列中的条件时进行计数

标签 python pandas dataframe

目前,我的数据框中有一列名为 step 的列,我想用它来设置计数器。它包含一堆重复的数字。我想为此创建一个新列,该列有一个计数器,当满足特定条件时该计数器会递增。条件是当step列中的数字第四次变化时,计数器将加1,然后重复该过程。这是我的代码示例以及我想要实现的目标:

df = pd.DataFrame({"step": [1,1,1,2,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,6,7,7,7,7,8,8,8,8,8,9,9,9,9,7,7,
7,8,8,8,9,9,7,7,8,8,8,9,9,9,7]})

df['counter'] = df['step'].cumsum() #This will increment when it sees a fourth different number, and repeat

理想情况下,我的输出如下所示:

print(df['step'])

[1,1,1,2,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,6,7,7,7,7,8,8,8,8,8,9,9,9,9,7,7,
7,8,8,8,9,9,7,7,8,8,8,9,9,9,7,7]

print(df['counter'])
[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,
3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5]

步骤中的数字会有所不同,但当序列中的第四个不同值被识别并重置计数器时,计数器将始终递增。我知道我可能可以使用 if 语句来做到这一点,但是我的数据框很大,如果可能的话,我宁愿以更快的比较方式来做到这一点。任何帮助将不胜感激!

最佳答案

您可以将step列转换为类别,然后根据类别代码进行计数:

import pandas as pd

df = pd.DataFrame({"step": [1,1,1,2,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,6,7,7,7,7,8,8,8,8,8,9,9,9,9,10]})
df["counter"] = df.step.astype("category").values.codes // 3

结果:

    step  counter
0      1        0
1      1        0
2      1        0
3      2        0
4      2        0
5      2        0
6      2        0
7      3        0
8      3        0
9      3        0
10     4        1
11     4        1
12     4        1
13     5        1
14     5        1
15     5        1
16     5        1
17     6        1
18     6        1
19     6        1
20     7        2
21     7        2
22     7        2
23     7        2
24     8        2
25     8        2
26     8        2
27     8        2
28     8        2
29     9        2
30     9        2
31     9        2
32     9        2
33    10        3

更新更改的数据(参见评论):
df = pd.DataFrame({"step": [1,1,1,2,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,6,7,7,7,7,8,8,8,8,8,9,9,9,9,7,7,7,8,8,8,9,9,7,7,8,8,8,9,9,9,7,7]})
df['counter'] = (df.step.diff().fillna(0).ne(0).cumsum() // 3).astype(int)

    step  counter
0      1        0
1      1        0
2      1        0
3      2        0
4      2        0
5      2        0
6      2        0
7      3        0
8      3        0
9      3        0
10     4        1
11     4        1
12     4        1
13     5        1
14     5        1
15     5        1
16     5        1
17     6        1
18     6        1
19     6        1
20     7        2
21     7        2
22     7        2
23     7        2
24     8        2
25     8        2
26     8        2
27     8        2
28     8        2
29     9        2
30     9        2
31     9        2
32     9        2
33     7        3
34     7        3
35     7        3
36     8        3
37     8        3
38     8        3
39     9        3
40     9        3
41     7        4
42     7        4
43     8        4
44     8        4
45     8        4
46     9        4
47     9        4
48     9        4
49     7        5
50     7        5

关于python - 创建一个计数器,迭代数据帧中的列,并在满足列中的条件时进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72978752/

相关文章:

python - 剧情: color all larger than different color

Python,通过正则表达式找到(n)n字符串的长度

python - 使用 apply() 自定义函数创建新列时出现 Pandas 内存错误

python - 如何一次修改多列?

python-3.x - 基于 Spacy 规则匹配器选择 Pandas DataFrame 的行

Python findall 使用正则表达式抓取 HTML 标签内的数据

python - 如何释放(ana)conda占用的磁盘空间?

python - Pandas 数据框 : get maxima of a column after grouping by another coumn

python pandas 日期转换为单词

python - 如何计算 pandas 数据框中各列的非 NaN 值?