简体   繁体   中英

Groupby a column, sort another column and assign rank to the rows in python

I have following dataframe 'scores' in pandas.

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

I would like sort the score column within each identifier and assign a rank to each entry. I am using following command to do that but not sure how to give rank.

score_new=scores.groupby(['identifier'],as_index=False).apply(lambda x:x.sort_values(by='score',ascending=False)).reset_index(drop=True)

The desired output is:

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 help here is highly appreciated

用:

df['rank']=df.groupby('identifier')['score'].rank(ascending=False).astype('int32')

Try this?

df1=df1.sort_values(['Identifier'],ascending=[True,True])
df1['Score']=df1.Name!=df1.Name.shift().fillna('edit')
df1.Score=df1.groupby('ID').Score.cumsum()+1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM