简体   繁体   English

在熊猫数据框中创建增量编号列

[英]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. 我需要在现有列的基础上按其值升序添加一个新的列分配数字,该数字介于1 -5之间。

You can use Series.rank with method 'dense' which assigns rank from minimum to maximum incrementing by 1 between groups 您可以将Series.rank与方法'dense'结合使用,该方法将组之间的最小到最大等级递增1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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