16

I have a dataframe of 2000 rows and 500 columns. I want to sort every column in ascending order. The columns don't have names they're just numbered 0-500.

Random data:

df = pandas.DataFrame(
    np.random.randint(0, 100, size=(2000, 500)),
    columns=range(500))

Using df.sort_values(by=0, axis=0) sorts the 0th column, as expected. But then using df.sort_values(by=1, axis=0) sorts the 1st column but shuffles the 0th column again. In other words, I want

index  0  1  2
1      5  5  5
2      6  7  5
3      7  9  8

But I can only ever get one column sorted at a time. I've tried this but it throws a key error.

df.sort_values(by=df.columns[0:524], axis=0)
1
  • could someone post an equivalent scala answer? Commented Nov 19, 2021 at 15:31

5 Answers 5

18

I think the most elegant solution nowadays is df.transform(np.sort).

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be considerably faster than using sort_values
13

I think you can use numpy.sort with DataFrame constructor or apply with sort_values with convert to numpy array by values:

df = pd.DataFrame(np.sort(df.values, axis=0), index=df.index, columns=df.columns)

Another solution, slowier:

df = df.apply(lambda x: x.sort_values().values)

print (df)
      0    1    2    3    4    5    6    7    8    9   ...   490  491  492  \
0       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
1       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
2       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
3       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
4       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
5       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
6       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
7       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
8       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
9       0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
10      0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
11      0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
12      0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
13      0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
14      0    0    0    0    0    0    0    0    0    0 ...     0    0    0   
15      0    0    0    0    0    1    0    0    0    0 ...     0    0    0   
16      0    0    0    0    0    1    1    0    0    0 ...     0    0    0   
17      0    0    0    0    0    1    1    0    0    0 ...     0    0    0   
18      0    0    0    0    0    1    1    0    0    0 ...     0    0    0   
19      0    0    0    0    0    1    1    1    1    0 ...     0    0    0   
20      0    0    1    0    0    1    1    1    1    0 ...     0    0    0   
21      0    0    1    0    0    1    1    1    1    1 ...     0    1    0   
22      0    1    1    0    0    1    1    1    1    1 ...     0    1    0   
23      1    1    1    0    0    1    1    1    1    1 ...     0    1    0   
24      1    1    1    0    0    1    1    1    1    1 ...     0    1    0   
25      1    1    1    1    0    1    1    1    1    1 ...     0    1    0   
26      1    1    1    1    0    1    1    1    1    1 ...     1    1    1   
27      1    1    1    1    0    1    1    1    1    1 ...     1    1    1   
28      1    1    1    1    0    1    1    1    1    1 ...     1    1    1   
29      1    1    1    1    0    1    1    1    1    1 ...     1    1    1   
...   ...  ...  ...  ...  ...  ...  ...  ...  ...  ... ...   ...  ...  ...   
1970   97   98   98   98   98   98   99   98   98   98 ...    98   98   98   
1971   97   98   98   98   98   98   99   98   98   98 ...    98   98   98   
1972   98   98   98   98   98   98   99   98   98   98 ...    98   98   98   
1973   98   98   98   99   98   98   99   98   98   98 ...    98   98   98   
1974   98   98   98   99   98   98   99   98   98   98 ...    98   98   98   
1975   98   98   98   99   98   98   99   98   98   98 ...    98   98   98   
1976   98   98   98   99   98   98   99   98   99   99 ...    98   98   98   
1977   98   98   98   99   98   98   99   98   99   99 ...    98   98   99   
1978   98   98   98   99   98   98   99   98   99   99 ...    98   98   99   
1979   98   98   98   99   99   99   99   98   99   99 ...    98   98   99   
1980   98   98   98   99   99   99   99   98   99   99 ...    98   98   99   
1981   99   99   98   99   99   99   99   98   99   99 ...    99   98   99   
1982   99   99   98   99   99   99   99   98   99   99 ...    99   98   99   
1983   99   99   98   99   99   99   99   98   99   99 ...    99   98   99   
1984   99   99   98   99   99   99   99   99   99   99 ...    99   99   99   
1985   99   99   98   99   99   99   99   99   99   99 ...    99   99   99   
1986   99   99   98   99   99   99   99   99   99   99 ...    99   99   99   
1987   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1988   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1989   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1990   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1991   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1992   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1993   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1994   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1995   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1996   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1997   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1998   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   
1999   99   99   99   99   99   99   99   99   99   99 ...    99   99   99   

      493  494  495  496  497  498  499  
0       0    0    0    0    0    0    0  
1       0    0    0    0    0    0    0  
2       0    0    0    0    0    0    0  
3       0    0    0    0    0    0    0  
4       0    0    0    0    0    0    0  
5       0    0    0    0    0    0    0  
6       0    0    0    0    0    0    0  
7       0    0    0    0    0    0    0  
8       0    0    0    0    0    0    0  
9       0    0    0    0    0    0    0  
10      0    0    0    0    0    0    0  
11      0    0    0    0    0    0    0  
12      0    0    0    0    0    0    0  
13      0    0    0    0    0    0    0  
14      0    0    0    0    0    0    0  
15      0    0    0    0    1    0    0  
16      0    1    0    0    1    0    0  
17      0    1    0    0    1    0    0  
18      1    1    0    0    1    0    0  
19      1    1    1    0    1    0    0  
20      1    1    1    0    1    0    1  
21      1    1    1    0    1    0    1  
22      1    1    1    0    1    0    1  
23      1    1    1    0    1    0    1  
24      1    1    1    0    1    0    1  
25      1    1    1    0    1    0    1  
26      1    1    1    0    1    0    1  
27      1    1    1    1    1    0    1  
28      1    1    1    1    1    0    1  
29      1    1    1    1    1    0    1  
...   ...  ...  ...  ...  ...  ...  ...  
1970   98   98   98   98   98   98   98  
1971   98   98   98   98   98   98   98  
1972   98   98   98   98   98   98   98  
1973   98   98   98   98   98   98   98  
1974   98   98   98   99   98   98   98  
1975   98   98   98   99   98   98   98  
1976   99   98   98   99   98   98   98  
1977   99   98   98   99   98   98   98  
1978   99   98   98   99   99   98   98  
1979   99   99   98   99   99   98   98  
1980   99   99   98   99   99   99   99  
1981   99   99   98   99   99   99   99  
1982   99   99   98   99   99   99   99  
1983   99   99   99   99   99   99   99  
1984   99   99   99   99   99   99   99  
1985   99   99   99   99   99   99   99  
1986   99   99   99   99   99   99   99  
1987   99   99   99   99   99   99   99  
1988   99   99   99   99   99   99   99  
1989   99   99   99   99   99   99   99  
1990   99   99   99   99   99   99   99  
1991   99   99   99   99   99   99   99  
1992   99   99   99   99   99   99   99  
1993   99   99   99   99   99   99   99  
1994   99   99   99   99   99   99   99  
1995   99   99   99   99   99   99   99  
1996   99   99   99   99   99   99   99  
1997   99   99   99   99   99   99   99  
1998   99   99   99   99   99   99   99  
1999   99   99   99   99   99   99   99  

4 Comments

TheBamf's answer is more concise and elegantly achieve the same desired result
@FlorianH - yes,more elegant but if performance is important then it is slowier, because processing each columns separately, in this answer are processing all values together, so faster.
@FlorianH - I get this timings In [177]: %timeit pd.DataFrame(np.sort(df.values, axis=0), index=df.index, columns=df.columns) 524 µs ± 31.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) In [178]: %timeit df.transform(np.sort) 1.68 ms ± 17.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
@jezrael - correct, your first proposition is 2 times faster than TheBamf's answer, on my machine (0.05 sec vs. 0.1 for the question author's example) . Good to know, thanks!
10
>>> df.sort_values(by=list(df.columns),axis=0)
       0  1  2
index         
1      5  5  5
2      6  7  5
3      7  9  8

Comments

2

To sort all columns of a dataframe, as an alternative to by=list(df.columns), you can use by=df.columns.to_list() as below:

df.sort_values(by=df.columns.to_list())

Docs:

Comments

0
df.sort(['col1','col2', ..., 'colN'],ascending=False)

or

df.sort(list(df.columns),ascending=False)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.