Home > Blockchain >  Pandas -> DataFrame -> rank
Pandas -> DataFrame -> rank

Time:10-10

I have the DataFrame, where I am trying to add a new "rank" column to determine the price rating relative to the "name" and "country" columns by comparing prices (column 'price'). But with no success. I need the following result, screenshot below. Help please, I will be grateful enter image description here

CodePudding user response:

Try this:

df['rank'] = df.groupby('name')['price'].rank(ascending=False)

CodePudding user response:

IIUC, first you need to sort price in each group then use rank like below:

df['rank'] = df.groupby('name')['price'].apply(lambda x: x.sort_values().rank())
  • Related