简体   繁体   中英

Create incremental number column in pandas dataframe

I have a dataframe with incremental float values

   Number
0 0.679484 
1 0.079027 
2 0.003132 
3 0.092761 
4 0.055500 
5 0.055500 
6 0.055500 
7 0.003132 

i need to add a new column assigning number between 1 -5 based on existing column on the basis of their values in ascending order.

You can use Series.rank with method 'dense' which assigns rank from minimum to maximum incrementing by 1 between groups

df['rank'] = df['Number'].rank(method = 'dense').astype(int)


    Number      rank
0   0.679484    5
1   0.079027    3
2   0.003132    1
3   0.092761    4
4   0.055500    2
5   0.055500    2
6   0.055500    2
7   0.003132    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