I have a df and I am trying to sort the columns in descending order based on the value in the last row (basically the value in the most left, down cell in the df). How can I do that in Python?
I have tried to search for a similar problem to mine but I have not managed to find a solution.
Thank you.
The resulting df should have in cell df[-1,1] the greatest value in the row, in df[-1,2] the second greatest...and so on.
you could use argsort()
on the negative values of the relevant row for this
import pandas as pd
import numpy as np
df.iloc[:,np.argsort(-df.iloc[-1,:])]
My understanding is you're trying to sort data based on the last column. I imagine this last column would be dynamic so you can't just use the column's name. For sorting any dataframe in descending order, you can use
df.sort_values([column name], ascending=False)
for finding the last column, you can use
df.columns[-1]
Putting that together, I think you want something like this.
df.sort_values(df.columns[-1], ascending=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.