I have a data frame with 3 columns:
ID | LayerID | ObjectID
I’d like to create an autonumber column that resets each time based on the combination of the 3 columns.
I thought I could use the ‘set_index’ and have been trying many variations:
df.set_index([‘ID’,’LayerID’,’ObjectID’])
but this just creates an index that doesn’t reset based on the index columns.
I must have spend a good 2 hours and I just can’t get it to work.
My dataframe is:
ID | LayerID | ObjectID |
---|---|---|
1 | A | A |
1 | A | B |
1 | B | A |
2 | A | A |
2 | C | C |
My expected Output ultimately should be:
ID | LayerID | ObjectID | Position |
---|---|---|---|
1 | A | A | 1 |
1 | A | B | 2 |
1 | B | A | 3 |
2 | A | A | 1 |
2 | C | C | 2 |
The ‘Position’ isn’t based on any sorting, just literally the order of the current dataframe.
Steve Allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.