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.
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: