在pandas数据框中对每一行进行排序的最快方法

Luk*_*uke 15 python performance pandas

我需要找到最快的方法来对数据帧中的每一行进行排序,其中包含数百万行和大约一百列.

所以像这样:

A   B   C   D
3   4   8   1
9   2   7   2
Run Code Online (Sandbox Code Playgroud)

需要成为:

A   B   C   D
8   4   3   1
9   7   2   2
Run Code Online (Sandbox Code Playgroud)

现在我正在对每一行应用sort并逐行构建一个新的数据帧.我也在为每一行做一些额外的,不太重要的事情(因此我为什么要使用熊猫而不是numpy).是否可以更快地创建列表列表,然后立即构建新的数据帧?或者我需要去cython吗?

And*_*den 18

我想我会在numpy中这样做:

In [11]: a = df.values

In [12]: a.sort(axis=1)  # no ascending argument

In [13]: a = a[:, ::-1]  # so reverse

In [14]: a
Out[14]:
array([[8, 4, 3, 1],
       [9, 7, 2, 2]])

In [15]: pd.DataFrame(a, df.index, df.columns)
Out[15]:
   A  B  C  D
0  8  4  3  1
1  9  7  2  2
Run Code Online (Sandbox Code Playgroud)

我原以为这可能有用,但它对列进行了排序:

In [21]: df.sort(axis=1, ascending=False)
Out[21]:
   D  C  B  A
0  1  8  4  3
1  2  7  2  9
Run Code Online (Sandbox Code Playgroud)

啊,熊猫提出:

In [22]: df.sort(df.columns, axis=1, ascending=False)
Run Code Online (Sandbox Code Playgroud)

ValueError:按列排序时,轴必须为0(行)


Spm*_*pmP 5

要添加到@ Andy-Hayden给出的答案中,要在整个框架中执行此操作...不确定该为什么有效,但确实可以。订单似乎没有控制权。

    In [97]: A = pd.DataFrame(np.random.randint(0,100,(4,5)), columns=['one','two','three','four','five'])

    In [98]: A
    Out[98]: 
    one  two  three  four  five
    0   22   63     72    46    49
    1   43   30     69    33    25
    2   93   24     21    56    39
    3    3   57     52    11    74

    In [99]: A.values.sort
    Out[99]: <function ndarray.sort>

    In [100]: A
    Out[100]: 
    one  two  three  four  five
    0   22   63     72    46    49
    1   43   30     69    33    25
    2   93   24     21    56    39
    3    3   57     52    11    74

    In [101]: A.values.sort()

    In [102]: A
    Out[102]: 
    one  two  three  four  five
    0   22   46     49    63    72
    1   25   30     33    43    69
    2   21   24     39    56    93
    3    3   11     52    57    74
    In [103]: A = A.iloc[:,::-1]

    In [104]: A
    Out[104]: 
    five  four  three  two  one
    0    72    63     49   46   22
    1    69    43     33   30   25
    2    93    56     39   24   21
    3    74    57     52   11    3
Run Code Online (Sandbox Code Playgroud)

我希望有人能解释这个原因,但很高兴它能起作用8)


Pra*_*ani 5

您可以使用 pd.apply。

Eg:

A = pd.DataFrame(np.random.randint(0,100,(4,5)),
        columns=['one','two','three','four','five']) 
print (A)

   one  two  three  four  five
0    2   75     44    53    46
1   18   51     73    80    66
2   35   91     86    44    25
3   60   97     57    33    79

B = A.apply(np.sort, axis = 1) 
print(B)

   one  two  three  four  five
0    2   44     46    53    75
1   18   51     66    73    80
2   25   35     44    86    91
3   33   57     60    79    97
Run Code Online (Sandbox Code Playgroud)

由于您希望按降序排列,因此只需将数据框乘以 -1 并对其进行排序即可。

A = pd.DataFrame(np.random.randint(0,100,(4,5)),
        columns=['one','two','three','four','five'])
A = A * -1
A = A.apply(np.sort, axis = 1)
A = A * -1
Run Code Online (Sandbox Code Playgroud)