[英]Sort DataFrame columns according to value in [last row, first column]
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). 我有一个df,我试图根据最后一行中的值(基本上是df中最左下方的单元格中的值)以降序对列进行排序。 How can I do that in Python? 如何在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. 结果df在单元格df [-1,1]中应具有该行的最大值,在df [-1,2]中应具有第二个最大值……依此类推。
you could use argsort()
on the negative values of the relevant row for this 您可以argsort()
在相关行的负值上使用argsort()
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.