2015-05-24 8 views
5

Ho un dataframe con ID asta e prezzi di offerta. Il dataframe è ordinato per id asta (ascendente) e il prezzo all'acquisto (decrescente):I panda classificano per il valore di colonna

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

vorrei aggiungere una colonna chiamata 'Auction_Rank' che classifica ID Asta del dall'aumento dei prezzi di offerta:

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 

risposta

16

Ecco un modo per farlo in modo Pandas-

Potreste groupby su Auction_ID e prendere rank() su Bid_Price con 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 
Problemi correlati