Question

In this way

import pandas
df = pandas.DataFrame({'col':['bbb','aaa','aaa','ccc']})
print df
print '-------------'
df['ranked'] = df['col'].rank(method='min')
print df

I get:

   col
0  bbb
1  aaa
2  aaa
3  ccc
-------------
   col  ranked
0  bbb       3
1  aaa       1
2  aaa       1
3  ccc       4

And I would like to have:

   col
0  bbb
1  aaa
2  aaa
3  ccc
-------------
   col  ranked
0  bbb       2
1  aaa       1
2  aaa       1
3  ccc       3

Does anybody know how to get it?

Was it helpful?

Solution

Try this:

import pandas
df = pandas.DataFrame({'col':['bbb','aaa','aaa','ccc']})
print df
print '-------------'

df2 = df.drop_duplicates()
df2['ranked'] = df2.rank(method='min')
print df.merge(df2, on='col')

Output:

   col
0  bbb
1  aaa
2  aaa
3  ccc
-------------
   col  ranked
0  bbb       2
1  aaa       1
2  aaa       1
3  ccc       3

OTHER TIPS

You can also use Series.rank(method="dense"), which is like min but just doesn't skip numbers:

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.Series.rank.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top