You can use groupby
with rank
with method min
to get the groupwise rankings based on smallest to largest value. Next you can sort_values
by this ranking and the group.
df = pd.DataFrame({'A':['a','b','a','a','b'],'B':[100,100,120,150,200]})
df['subindex'] = df.groupby('A')['B'].rank(method='min')
df = df.sort_values(['A','subindex'])
print(df)
A B subindex
0 a 100 1.0
2 a 120 2.0
3 a 150 3.0
1 b 100 1.0
4 b 200 2.0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…