繁体   English   中英

按 Pandas Dataframe 中值的计数向组中添加数字

[英]Add number to groups by count of values in Pandas Dataframe

我有一个 pandas Dataframe 的列,我想将它们按 3 行的包分组,然后在每个包上递增一个索引。

id      protocol    protocol_grp
1       ISD     ISD1
2       ISD     ISD1
3       ISD     ISD1
4       IRQ     IRQ1
5       IRQ     IRQ1
6       IRQ     IRQ1
7       IRQ     IRQ2
8       IRQ     IRQ2
9       IRQ     IRQ2
10      IRQ     IRQ3
11      ISD     ISD2
12      ISD     ISD2
13      ISD     ISD2
14      ISD     ISD3
15      IRQ     IRQ3
16      IRQ     IRQ3
17      IRQ     IRQ4

所需的 output 是 protocol_grp 列。 我希望能够做的是每次我有 3 个相同的协议时,我将索引增加 1。

希望这是有道理的。

您可以使用:

df['protocol_grp'] = df['protocol'] + df.groupby('protocol').cumcount() \
                                        .floordiv(3).add(1).astype(str)
print(df)

# Output
    id protocol protocol_grp
0    1      ISD         ISD1
1    2      ISD         ISD1
2    3      ISD         ISD1
3    4      IRQ         IRQ1
4    5      IRQ         IRQ1
5    6      IRQ         IRQ1
6    7      IRQ         IRQ2
7    8      IRQ         IRQ2
8    9      IRQ         IRQ2
9   10      IRQ         IRQ3
10  11      ISD         ISD2
11  12      ISD         ISD2
12  13      ISD         ISD2
13  14      ISD         ISD3
14  15      IRQ         IRQ3
15  16      IRQ         IRQ3  # <- check this row
16  17      IRQ         IRQ4

让我们检查cumcount然后得到除数

df['protocol_grp'] = df['protocol'].add((df.groupby('protocol').cumcount()//3+1).astype(str))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM