简体   繁体   English

根据[最后一行,第一列]中的值对DataFrame列进行排序

[英]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.

相关问题 pandas dataframe按列总计排序列 - pandas dataframe sort columns according to column totals 根据列值对带有MultiIndex的pandas DataFrame进行排序 - Sort pandas DataFrame with MultiIndex according to column value 设置数据框中列的第一行和最后一行 - Set first and last row of a column in a dataframe 如何删除熊猫数据框的最后一列中的第一个值,然后删除其余的最后一行? - How to delete first value in the last column of a pandas dataframe and then delete the remaining last row? 根据字符串值列对熊猫数据框行进行排序 - Sort pandas dataframe rows according to a string value column Python:从数据帧的列中删除除我们存储在第一行中的最后一个值之外的所有数据 - Python : Remove all data from a column of a dataframe except the last value that we store in the first row Python:删除dataframe某列的所有数据,保留第一行最后一个值 - Python : Remove all data in a column of a dataframe and keep the last value in the first row 熊猫数据框:如何根据行中的值复制其他列 - panda dataframe: how to copy some columns in others according to a value in the row Pandas - 删除重复项但根据列中的值更改 keep:first/last - Pandas - Drop duplicates but change keep:first/last according to a value in a column Python,根据每行第一列的值替换最后N列的所有整数 - Python, replace all integers in the last N columns based on the value in the first column for each row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM