Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a ranking ordered column to a Pandas Dataframe

I know this question may seem trivial, but I can't find the solution anywhere. I have a really large pandas dataframe df that looks something like this:

                                            conference     IF2013  AR2013
0                                            HOTMOBILE  16.333333   31.50
1                                                 FOGA  13.772727   60.00
2                                              IEA/AIE  10.433735   28.20
3    IEEE Real-Time and Embedded Technology and App...  10.250000   29.00
4                  Symposium on Computational Geometry   9.880342   35.00
5                                                 WISA   9.693878   43.60
6                                                 ICMT   8.750000   22.00
7                                              Haskell   8.703704   39.00

I would like to add an extra column at the end that orders it 1,2,3,4, etc. So it looks like this:

                                               conference     IF2013    AR2013  Ranking 
    0                                            HOTMOBILE  16.333333   31.50   1  
    1                                                 FOGA  13.772727   60.00   2
    2                                              IEA/AIE  10.433735   28.20   3
    3    IEEE Real-Time and Embedded Technology and App...  10.250000   29.00   4  

I can't seem to figure out how to add a filled extra column that just put a Series of consecutive numbers.

like image 668
BKS Avatar asked Oct 27 '25 10:10

BKS


1 Answers

You can try:

df['rank'] = df.index + 1

print df
#                                          conference     IF2013  AR2013  rank
#0                                          HOTMOBILE  16.333333    31.5     1
#1                                               FOGA  13.772727    60.0     2
#2                                            IEA/AIE  10.433735    28.2     3
#3  IEEE Real-Time and Embedded Technology and App...  10.250000    29.0     4
#4                Symposium on Computational Geometry   9.880342    35.0     5
#5                                               WISA   9.693878    43.6     6
#6                                               ICMT   8.750000    22.0     7
#7                                            Haskell   8.703704    39.0     8

Or use rank with parameter ascending=False:

df['rank'] = df['conference'].rank(ascending=False)
print df
#                                          conference     IF2013  AR2013  rank
#0                                          HOTMOBILE  16.333333    31.5     1
#1                                               FOGA  13.772727    60.0     2
#2                                            IEA/AIE  10.433735    28.2     3
#3  IEEE Real-Time and Embedded Technology and App...  10.250000    29.0     4
#4                Symposium on Computational Geometry   9.880342    35.0     5
#5                                               WISA   9.693878    43.6     6
#6                                               ICMT   8.750000    22.0     7
#7                                            Haskell   8.703704    39.0     8
like image 159
jezrael Avatar answered Oct 30 '25 00:10

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!