Fastest way to sort each row in a pandas dataframe

后端 未结 5 2234
温柔的废话
温柔的废话 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:36

    One could try this approach to preserve the integrity of the df:

    import pandas as pd 
    import numpy as np
    
    A = pd.DataFrame(np.random.randint(0,100,(4,5)), columns=['one','two','three','four','five']) 
    print (A) 
    print(type(A))
    
       one  two  three  four  five
    0   85   27     64    50    55
    1    3   90     65    22     8
    2    0    7     64    66    82
    3   58   21     42    27    30
    
    
    B = A.apply(lambda x: np.sort(x), axis=1, raw=True) 
    print(B) 
    print(type(B))
    
       one  two  three  four  five
    0   27   50     55    64    85
    1    3    8     22    65    90
    2    0    7     64    66    82
    3   21   27     30    42    58
    
    

提交回复
热议问题