以这种方式
import pandas
df = pandas.DataFrame({'col':['bbb','aaa','aaa','ccc']})
print df
print '-------------'
df['ranked'] = df['col'].rank(method='min')
print df
我明白了:
col
0 bbb
1 aaa
2 aaa
3 ccc
-------------
col ranked
0 bbb 3
1 aaa 1
2 aaa 1
3 ccc 4
我想:
col
0 bbb
1 aaa
2 aaa
3 ccc
-------------
col ranked
0 bbb 2
1 aaa 1
2 aaa 1
3 ccc 3
有人知道如何获得它吗?
答案 0 :(得分:1)
试试这个:
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')
输出:
col
0 bbb
1 aaa
2 aaa
3 ccc
-------------
col ranked
0 bbb 2
1 aaa 1
2 aaa 1
3 ccc 3
答案 1 :(得分:1)
你也可以使用Series.rank(method="dense")
,这就像min一样,但不会跳过数字:
http://pandas.pydata.org/pandas-docs/dev/generated/pandas.Series.rank.html