Mar Mar - 1 month ago 15
Python Question

Sort weightly a dataframe according to two columns

I have a pandas dataframe and i want to order it based in two columns. With

df.sort_values(['col1', 'col2'], ascending=False)
it sorts first with the col1 and then with col2. However what I wanted to do is to sort giving the same importance to both columns which is the sort was done taking into account both columns instead of one first and then the other one. How can I do this ?

Edit: col1 is float and represents a percentage and col2 is also a float but its not a percentage

Wen Wen
Answer Source

Try this ?

df['Rank']=df.col1.rank()+df.col2.rank()
df.sort_values('Rank',ascending=False).drop('Rank',axis=1)