我在熊猫中有一个表达,我按国家对前三个值进行排序:
Country | Value
---------------------|------
Germany | 102.1
Germany | 90.3
Germany | 44.6
Switzerland | 59.9
Switzerland | 35.3
Switzerland | 21.6
...and so on
这是我用df.groupby("Country").head(3)[["Country", "Value"]]
得到的。现在,我想添加第三列,将国家内的排名与值关联:
Country | Value | Rank
---------------------|--------|------
Germany | 102.1 | 1
Germany | 90.3 | 2
Germany | 44.6 | 3
Switzerland | 59.9 | 1
Switzerland | 35.3 | 2
Switzerland | 21.6 | 3
...and so on
我最好怎么做呢
我相信您需要^{} 和
method='dense'
,因为通过转换为integers
列的排序值,组之间的排名始终增加1:如果需要计数器,则最好使用^{} :
变化的数据最能体现差异:
相关问题 更多 >
编程相关推荐