Fastest way to sort each row in a pandas dataframe

后端 未结 5 2244
温柔的废话
温柔的废话 2020-12-01 21:06

I need to find the quickest way to sort each row in a dataframe with millions of rows and around a hundred columns.

So something like this:

A   B   C         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 21:17

    To Add to the answer given by @Andy-Hayden, to do this inplace to the whole frame... not really sure why this works, but it does. There seems to be no control on the order.

        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]: 
    
        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
    

    I hope someone can explain the why of this, just happy that it works 8)

提交回复
热议问题