我在熊猫中有以下数据框的“分数”。
identifier name score
D29650 abc10 115369-52-3 0.75
D29650 abc10 115369-52-4 0.34
D29651 abc11 115369-52-5 0.25
D29651 abc11 ls-132190 0.67
D29652 abc12 me1228 1.0
D29652 abc12 me 1228 0.875
我想对每个标识符中的分数列进行排序,并为每个条目分配一个等级。我正在使用以下命令来做到这一点,但不确定如何给排名。
score_new=scores.groupby(['identifier'],as_index=False).apply(lambda x:x.sort_values(by='score',ascending=False)).reset_index(drop=True)
所需的输出是:
identifier name score rank
D29650 abc10 115369-52-3 0.75 1
D29650 abc10 115369-52-4 0.34 2
D29651 abc11 ls-132190 0.67 1
D29651 abc11 115369-52-5 0.25 2
D29652 abc12 me1228 1.0 1
D29652 abc12 me 1228 0.875 2
Anh的帮助在这里受到高度赞赏
采用:
df['rank']=df.groupby('identifier')['score'].rank(ascending=False).astype('int32')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句