Reputation: 13
I need to sort pandas df by values without priority of particular column. I've attached the image with expected result. In this case, index '2' has 0,99 value, so we put it at the first place. Then, we put '4' at the second row because it contains 0,95 value(independently of exact column) etc. How can I apply such kind of sorting?
Upvotes: 1
Views: 223
Reputation: 18315
You can get the maximum values of each row, then get the indices that would sort them in descending order with np.argsort
and then index into your dataframe with those:
sorted_df = df.iloc[np.argsort(df.max(axis=1))[::-1]]
where np.argsort
gives the indices that would sort the maximum values of each row but in ascending order; so we use [::-1]
to reverse that to get descending order. iloc
performs the integer indexing,
to get
# sample dataframe
np.random.seed(51)
df = pd.DataFrame({"a": np.random.random(size=5),
"b": np.random.random(size=5),
"c": np.random.random(size=5)})
>>> df
a b c
0 0.675731 0.949338 0.174343
1 0.044712 0.157670 0.218183
2 0.343304 0.387973 0.646746
3 0.644020 0.589994 0.249831
4 0.284213 0.487796 0.410248
>>> sorted_df
a b c
0 0.675731 0.949338 0.174343
2 0.343304 0.387973 0.646746
3 0.644020 0.589994 0.249831
4 0.284213 0.487796 0.410248
1 0.044712 0.157670 0.218183
Upvotes: 1