Skip to content
Advertisement

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

I have this dataframe with which i am trying to create a new column rank on basis of increasing values of column Opportunity with 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

required output —

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

Advertisement

Answer

You can use rank function:

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

enter image description here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement