首页 > 解决方案 > Pandas.DataFrame:如何按每行中的最大值对行进行排序

问题描述

我有一个如图所示的数据框(word2vec 分析的结果)。我需要按每行中的最大值对行进行降序排序。所以我希望排序后的行顺序如图像中的红色数字所示。

谢谢

迈克尔

原始数据框和所需序列

标签: pythonpandasdataframe

解决方案


Find max on axis=1 and sort this series of maxes. reindex using this index.

Sample df

    A   B   C   D   E   F
0  95  86  29  38  79  18
1  15   8  34  46  71  50
2  29   9  78  97  83  45
3  88  25  17  83  78  77
4  40  82   3   0  78  38

df_final = df.reindex(df.max(1).sort_values(ascending=False).index)

Out[675]:
    A   B   C   D   E   F
2  29   9  78  97  83  45
0  95  86  29  38  79  18
3  88  25  17  83  78  77
4  40  82   3   0  78  38
1  15   8  34  46  71  50

推荐阅读