我想根据列KEY
中的值在列flag
中填写数字。
- 如果列
KEY
中的值保持不变,我希望每两行填充相同的数字,而不是使用cumcount()
填充递增的数字。 - 如果
KEY
列中的值更改,则填充的数字也会更改。
下面是示例,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']})
1条答案
按热度按时间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: