熊猫:按最大值对列进行排序

时间:2016-10-18 14:36:37

标签: sorting pandas max

假设我们有以下数据框:

// this is what I'm calling the "master template", this isn't an official term
template<class A, class B, ......., class N>
class Foo{};

template</*...This can have as many (or 0) entries as you need for this specialization...*/>
class Foo</*...This must have the same number as master template above, but can be broken down into as many smaller pieces as you want...*/> {};

在这里,我们需要根据列的最大值对列进行排序。 因此,列应按如下排序:

A  B  C  D

1  5  16 1
5  30 45 10
2  40 60 5
4  15 40 7

因为max(C)= 60,max(B)= 40,max(D)= 10,max(A)= 5.

自动化这种方法的最佳方法是什么?

1 个答案:

答案 0 :(得分:3)

您可以对df.max的结果进行排序,并使用它来重新索引df:

In [64]:
df.ix[:, df.max().sort_values(ascending=False).index]

Out[64]:
    C   B   D  A
0  16   5   1  1
1  45  30  10  5
2  60  40   5  2
3  40  15   7  4

打破上述情况:

In [66]:
df.max()

Out[66]:
A     5
B    40
C    60
D    10
dtype: int64

In [67]:
df.max().sort_values(ascending=False)

Out[67]:
C    60
B    40
D    10
A     5
dtype: int64