将顺序渐进 ID 分配为 pandas 系列更改中的值
我有以下 DataFrame:
date product_code discount
01/01/2022 1 0.7
01/01/2022 2 0.5
02/01/2022 1 0.1
02/01/2022 1 0.1
02/01/2022 2 0.5
03/01/2022 1 0.4
04/01/2022 1 0.1
04/01/2022 2 0.1
05/01/2022 1 0.1
06/01/2022 1 0.1
06/01/2022 1 0.5
...
我想在折扣率发生变化时,为每个“product_code”和折扣率组合有效地分配一个连续的渐进 ID。
因此,获得:
date product_code discount promotion_id
01/01/2022 1 0.7 1
01/01/2022 2 0.5 1
02/01/2022 1 0.1 2
02/01/2022 1 0.1 2
02/01/2022 2 0.5 1
03/01/2022 1 0.4 3
04/01/2022 1 0.1 4
04/01/2022 2 0.1 2
05/01/2022 1 0.1 4
06/01/2022 1 0.1 4
06/01/2022 1 0.5 5
...
为了更好地说明,对于单个产品案例,它将是:
date product_code discount promotion_id
01/01/2022 1 0.7 1
02/01/2022 1 0.1 2
02/01/2022 1 0.1 2
03/01/2022 1 0.4 3
04/01/2022 1 0.1 4
05/01/2022 1 0.1 4
06/01/2022 1 0.1 4
06/01/2022 1 0.5 5
...
我怎样才能实现这一点?
I have the following DataFrame:
date product_code discount
01/01/2022 1 0.7
01/01/2022 2 0.5
02/01/2022 1 0.1
02/01/2022 1 0.1
02/01/2022 2 0.5
03/01/2022 1 0.4
04/01/2022 1 0.1
04/01/2022 2 0.1
05/01/2022 1 0.1
06/01/2022 1 0.1
06/01/2022 1 0.5
...
And I would like to efficiently assign a sequential progressive ID, whenever the discount ratio changes, for each 'product_code' and discount ratio combination.
Thus, obtaining:
date product_code discount promotion_id
01/01/2022 1 0.7 1
01/01/2022 2 0.5 1
02/01/2022 1 0.1 2
02/01/2022 1 0.1 2
02/01/2022 2 0.5 1
03/01/2022 1 0.4 3
04/01/2022 1 0.1 4
04/01/2022 2 0.1 2
05/01/2022 1 0.1 4
06/01/2022 1 0.1 4
06/01/2022 1 0.5 5
...
To better illustrate, for a single product case it would be:
date product_code discount promotion_id
01/01/2022 1 0.7 1
02/01/2022 1 0.1 2
02/01/2022 1 0.1 2
03/01/2022 1 0.4 3
04/01/2022 1 0.1 4
05/01/2022 1 0.1 4
06/01/2022 1 0.1 4
06/01/2022 1 0.5 5
...
How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
groupby
中使用diff
和cumsum
进行检查You may check with
diff
withcumsum
withingroupby