简体   繁体   English

第二列中有条件的值计数

[英]Conditional cumcount of values in second column

I want to fill numbers in column flag , based on the value in column KEY .我想根据KEY列中的值在flag列中填充数字。

  • Instead of using cumcount() to fill incremental numbers, I want to fill same number for every two rows if the value in column KEY stays same.如果列KEY中的值保持不变,我想为每两行填充相同的数字,而不是使用cumcount()来填充增量数字。
  • If the value in column KEY changes, the number filled changes also.如果KEY列中的值发生变化,则填充的数字也会发生变化。

Here is the example, df1 is what I want from df0.这是示例,df1 是我想要的 df0。

df0 = pd.DataFrame({'KEY':['0','0','0','0','1','1','1','2','2','2','2','2','3','3','3','3','3','3','4','5','6']})

df1 = pd.DataFrame({'KEY':['0','0','0','0','1','1','1','2','2','2','2','2','3','3','3','3','3','3','4','5','6'],
                    'flag':['0','0','1','1','2','2','3','4','4','5','5','6','7','7','8','8','9','9','10','11','12']})

You want to get the cumcount and add one.你想获得 cumcount 并添加一个。 Then use %2 to differentiate between odd or even rows.然后使用%2区分奇数行或偶数行。 Then, take the cumulative sum and subtract 1 to start counting from zero.然后,取累加和减1,从零开始计数。

You can use:您可以使用:

df0['flag'] = ((df0.groupby('KEY').cumcount() + 1) % 2).cumsum() - 1
df0
Out[1]: 
   KEY  flag
0    0      0
1    0      0
2    0      1
3    0      1
4    1      2
5    1      2
6    1      3
7    2      4
8    2      4
9    2      5
10   2      5
11   2      6
12   3      7
13   3      7
14   3      8
15   3      8
16   3      9
17   3      9
18   4     10
19   5     11
20   6     12

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

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