I have this dataframe in Pandas:
id animal
0 dog
0 cat
1 goat
1 cow
1 sheep
1 pig
2 lion
2 tiger
2 bear
I want to add a column as follows. I don't know what you call it but basically it's an index for each unique id.
id animal ix
0 dog 0
0 cat 1
1 goat 0
1 cow 1
1 sheep 2
1 pig 3
2 lion 0
2 tiger 1
2 bear 2
Notice how it increments going down for whenever it continues to see the same id but it resets back to 0 whenever a new id has been found. Anyone knows the best way it could be done? I was thinking of obtaining all unique id's and increment an ix for each row of the same id that I see. Is there a better way to do it? (Side note, I shouldn't call it id since it's not unique in the df but I just can't think of a better column name)