第二列中值的条件累计计数

wtzytmuj  于 2022-09-18  发布在  Java
关注(0)|答案(1)|浏览(158)

我想根据列KEY中的值在列flag中填写数字。

  • 如果列KEY中的值保持不变,我希望每两行填充相同的数字,而不是使用cumcount()填充递增的数字。
  • 如果KEY列中的值更改,则填充的数字也会更改。

下面是示例,df1是我想要的df0。

  1. 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']})
  2. 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'],
  3. 'flag':['0','0','1','1','2','2','3','4','4','5','5','6','7','7','8','8','9','9','10','11','12']})
fykwrbwg

fykwrbwg1#

You want to get the cumcount and add one. Then use %2 to differentiate between odd or even rows. Then, take the cumulative sum and subtract 1 to start counting from zero.

You can use:

  1. df0['flag'] = ((df0.groupby('KEY').cumcount() + 1) % 2).cumsum() - 1
  2. df0
  3. Out[1]:
  4. KEY flag
  5. 0 0 0
  6. 1 0 0
  7. 2 0 1
  8. 3 0 1
  9. 4 1 2
  10. 5 1 2
  11. 6 1 3
  12. 7 2 4
  13. 8 2 4
  14. 9 2 5
  15. 10 2 5
  16. 11 2 6
  17. 12 3 7
  18. 13 3 7
  19. 14 3 8
  20. 15 3 8
  21. 16 3 9
  22. 17 3 9
  23. 18 4 10
  24. 19 5 11
  25. 20 6 12
展开查看全部

相关问题