I have a dataframe
df =
Col Val
a. 8
a. 9
c. 4
c. 0
d. 3
d. 9
I want to sort by Val of the smallest value within group and then foreach row get the index of the groupby Col So the new df will df
df_new =
Col Val Idx
c. 4. 0
c. 0. 0
d. 3. 1
d. 9. 1
a. 8. 2
a. 9. 2
What is the best way to do so?