Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas rank by column value [duplicate]

I have a dataframe that has auction IDs and bid prices. The dataframe is sorted by auction id (ascending) and bid price (descending):

Auction_ID    Bid_Price 123           9 123           7 123           6 123           2 124           3 124           2 124           1 125           1 

I'd like to add a column called 'Auction_Rank' that ranks auction id's by bid prices:

Auction_ID    Bid_Price    Auction_Rank 123           9            1 123           7            2 123           6            3 123           2            4 124           3            1 124           2            2 124           1            3 125           1            1 
like image 298
Christopher Jenkins Avatar asked May 24 '15 16:05

Christopher Jenkins


People also ask

How do you rank a column in pandas?

Pandas DataFrame: rank() functionThe rank() function is used to compute numerical data ranks (1 through n) along axis. By default, equal values are assigned a rank that is the average of the ranks of those values. Index to direct ranking.

How can check duplicate column in pandas?

To find duplicate columns we need to iterate through all columns of a DataFrame and for each and every column it will search if any other column exists in DataFrame with the same contents already. If yes then that column name will be stored in the duplicate column set.

How pandas handle duplicate columns?

To drop duplicate columns from pandas DataFrame use df. T. drop_duplicates(). T , this removes all columns that have the same data regardless of column names.


1 Answers

Here's one way to do it in Pandas-way

You could groupby on Auction_ID and take rank() on Bid_Price with ascending=False

In [68]: df['Auction_Rank'] = df.groupby('Auction_ID')['Bid_Price'].rank(ascending=False)  In [69]: df Out[69]:    Auction_ID  Bid_Price  Auction_Rank 0         123          9             1 1         123          7             2 2         123          6             3 3         123          2             4 4         124          3             1 5         124          2             2 6         124          1             3 7         125          1             1 
like image 138
Zero Avatar answered Sep 20 '22 07:09

Zero