如何根据 Pandas 中其他列的增加值添加新列排名

How to add a new column rank on based on increasing value of other column in Pandas

我有这个数据框,我正在尝试根据 Opportunity 列的增加值创建一个新列 rank pandas

State   Brand       DYA     Opportunity
Delhi   Pampers     -8.58   -1.24139
Delhi   Ariel       0.53    0.04800
Delhi   Fusion      0.68    0.00492
Delhi   Gillette    1.56    0.02073

需要输出--

State   Brand       DYA     Opportunity Rank
Delhi   Pampers     -8.58   -1.24139     1
Delhi   Ariel       0.53    0.04800      4      
Delhi   Fusion      0.68    0.00492      2
Delhi   Gillette    1.56    0.02073      3

您可以使用rank函数:

df['Rank'] = df['Opportunity'].rank()