2017-05-26 46 views
3

我所擁有的是超過2列的數據幀像這樣Python的數據幀等級系列

columnA columnB 
    1  a 
    1  b 
    1  c 
    2  d 

我只是想排名columnA這樣

columnA columnB rankA 
    1  a  1 
    1  b  2 
    1  c  3 
    2  d  1 

那麼,應該怎麼辦?

回答

0

使用groupby + cumcount

df['rankA'] = df.groupby('columnA').cumcount() + 1 
print (df) 
    columnA columnB rankA 
0  1  a  1 
1  1  b  2 
2  1  c  3 
3  2  d  1 
+0

THX,兄弟,它的工作原理 –

+0

很高興能幫助!如果我的回答很有幫助,請不要忘記[接受](http://meta.stackexchange.com/a/5235/295067)它。謝謝。 – jezrael