Fastest way to sort each row in a pandas dataframe

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

    Instead of using pd.DataFrame constructor, an easier way to assign the sorted values back is to use double brackets:

    original dataframe:

    A   B   C   D
    3   4   8   1
    9   2   7   2
    
    df[['A', 'B', 'C', 'D']] = np.sort(df)[:, ::-1]
    
       A  B  C  D
    0  8  4  3  1
    1  9  7  2  2
    

    This way you can also sort a part of the columns:

    df[['B', 'C']] = np.sort(df[['B', 'C']])[:, ::-1]
    
       A  B  C  D
    0  3  8  4  1
    1  9  7  2  2
    

提交回复
热议问题