Sort all columns of a pandas DataFrame independently using sort_values()

前端 未结 2 600
傲寒
傲寒 2020-12-06 21:17

I have a dataframe and want to sort all columns independently in descending or ascending order.

import pandas as pd

data = {\'a\': [5, 2, 3, 6],
        \'b         


        
相关标签:
2条回答
  • 2020-12-06 21:45

    sort_values will sort the entire data frame by the columns order you pass to it. In your first example you are sorting the entire data frame with ['a', 'b', 'c']. This will sort first by 'a', then by 'b' and finally by 'c'.

    Notice how, after sorting by a, the rows maintain the same. This is the expected result.

    Using lambda you are passing each column to it, this means sort_values will apply to a single column, and that's why this second approach sorts the columns as you would expect. In this case, the rows change.

    If you don't want to use lambda nor numpy you can get around using this:

    pd.DataFrame({x: df[x].sort_values().values for x in df.columns.values})
    

    Output:

       a  b  c
    0  2  1  1
    1  3  4  2
    2  5  7  4
    3  6  9  5
    
    0 讨论(0)
  • 2020-12-06 21:49

    You can use numpy.sort with DataFrame constructor:

    df1 = pd.DataFrame(np.sort(df.values, axis=0), index=df.index, columns=df.columns)
    print (df1)
       a  b  c
    0  2  1  1
    1  3  4  2
    2  5  7  4
    3  6  9  5
    

    EDIT:

    Answer with descending order:

    arr = df.values
    arr.sort(axis=0)
    arr = arr[::-1]
    print (arr)
    [[6 9 5]
     [5 7 4]
     [3 4 2]
     [2 1 1]]
    
    df1 = pd.DataFrame(arr, index=df.index, columns=df.columns)
    print (df1)
       a  b  c
    0  6  9  5
    1  5  7  4
    2  3  4  2
    3  2  1  1
    
    0 讨论(0)
提交回复
热议问题