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 then cumsum()
to automatically build a group id:
df['UID'] = (df['ID'] != df['ID'].shift() + 1).cumsum()
It gives as expected:
df
Name ID UID
0 T1 122 1
1 T1 123 1
2 T1 124 1
3 T1 125 1
4 T1 507 2
5 T1 508 2
6 T1 509 2
7 T1 510 2
8 T1 511 2