Pandas rank by multiple columns

后端 未结 5 716
小鲜肉
小鲜肉 2020-12-16 19:05

I am trying to rank a pandas data frame based on two columns. I can rank it based on one column, but how can to rank it based on two columns? \'SaleCount\', then \'TotalReve

5条回答
  •  悲哀的现实
    2020-12-16 19:33

    Another way would be to type-cast both the columns of interest to str and combine them by concatenating them. Convert these back to numerical values so that they could be differentiated based on their magnitude.

    In method=dense, ranks of duplicated values would remain unchanged. (Here: 6)

    Since you want to rank these in their descending order, specifying ascending=False in Series.rank() would let you achieve the desired result.

    col1 = df["SaleCount"].astype(str) 
    col2 = df["TotalRevenue"].astype(str)
    df['Rank'] = (col1+col2).astype(int).rank(method='dense', ascending=False).astype(int)
    df.sort_values('Rank')
    

提交回复
热议问题