使用python生成唯一ID
我应该如何将唯一的ID(UID)分配给一组行和不同的UID,而ID值突然增加。例如;将“ 1”分配给ID值范围为122至125,而“ 2”的UID在下表中为507到511。
名称 | ID | UID |
---|---|---|
T1 | 122 | |
T1 | 123 | |
T1 | 124 | |
T1 | 125 | |
T1 | 507 | |
T1 | 508 | |
T1 | 509 | |
T1 | 510 | |
T1 | 511 |
说实话,我不知道从哪里开始。
How should I assign a unique ID (UID) to a group of rows and a different UID where there is a sudden jump in ID values. For example; assign "1" to ID values ranging from 122 to 125 and a UID of "2" where ID values range from 507 to 511 in the table below.
Name | ID | UID |
---|---|---|
T1 | 122 | |
T1 | 123 | |
T1 | 124 | |
T1 | 125 | |
T1 | 507 | |
T1 | 508 | |
T1 | 509 | |
T1 | 510 | |
T1 | 511 |
To be honest i don't know where to start from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要 unique id,而只是 group iD,其中一个组是一组具有连续ID值的行。假设熊猫,您可以使用
shift()
识别连续值,然后cumsum()
自动构建组ID:预期的:
You do not want an unique id, but just a group id, where a group is a set of rows with consecutive ID values. Assuming pandas, you could use
shift()
to identify consecutive values, and thencumsum()
to automatically build a group id:It gives as expected: