Sort all columns of a dataframe

'dataframe' object has no attribute 'sort'
pandas dataframe sort by column
pandas sort by column name
pandas sort by multiple columns
pandas dataframe sort by index
how to sort columns in alphabetical order in dataframe
sort the dataframe in descending order of very interested.
pandas sort each column

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 df.sort_values(by=df.columns[0:524],axis=0) but that throws a key error.

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  

pandas.DataFrame.sort_values — pandas 1.1.0 documentation, bystr or list of str. Name or list of names to sort by. if axis is 0 or 'index' then by may contain index levels and/or column labels. if axis is 1 or 'columns' then by� Sort Dataframe rows based on columns in Descending Order To sort all the rows in above datafarme based on columns in descending order pass argument ascending with value False along with by arguments i.e. # Sort the rows of dataframe by column 'Name' in descending order dfObj = dfObj.sort_values(by ='Name', ascending=False)

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

pandas.DataFrame.sort — pandas 0.19.2 documentation, DataFrame. sort (columns=None, axis=0, ascending=True, inplace=False, Sort DataFrame either by labels (along either axis) or by the values in column(s)� 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: DataFrame.sort_values; Index.to_list, Index.tolist

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

Pandas: Sort rows or columns in Dataframe based on values using , Whereas, if inplace argument is True then it will make the current dataframe sorted. Let's understand by some examples,. First of all create a� To sort the rows of a DataFrame by a column, use pandas. DataFrame. sort_values () method with the argument by = column_name. The sort_values () method does not modify the original DataFrame, but returns the sorted DataFrame. You can sort the dataframe in ascending or descending order of the column values. In this tutorial, we shall go through some example programs, where we shall sort dataframe in ascending or descending order.

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

or

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

How to Sort Pandas DataFrame (with examples), In this short tutorial, you'll see 4 examples of sorting: A column in an ascending order; A column in a descending order; By multiple columns – Case 1; By multiple� Pandas Sort. Let’s take a look at the different parameters you can pass pd.DataFrame.sort_values(): by – Single name, or list of names, that you want to sort by. This can either be column names, or index names. Pass a list of names when you want to sort by multiple columns. axis (Default: ‘index’ or 0) – This is the axis to be sorted

6 ways to Sort Pandas Dataframe: Pandas Tutorial, How to sort pandas data frame by a column,multiple columns, and row? Often you want to sort Pandas data frame in a specific way. Typically� Example 3: Sort by multiple columns – case 1. But what if you want to sort by multiple columns? In that case, you may use the following template to sort by multiple columns: df.sort_values(by=['First Column','Second Column',], inplace=True) Suppose that you want to sort by both the ‘Year’ and the ‘Price.’ Since you have two records where the Year is 2018 (i.e., for the Ford Focus and Audi A4), then sorting by a second column – the ‘Price’ column – would be useful:

How to sort a Pandas DataFrame by multiple columns in Python, Call pandas.DataFrame.sort_values(by, ascending) with by as a list of column names to sort the rows in the DataFrame object based� R – Sorting a data frame by the contents of a column Posted on February 12, 2010 by i82much in R bloggers | 0 Comments [This article was first published on Developmentality » R , and kindly contributed to R-bloggers ].

Sort a dataframe in Pandas based on multiple columns, Sort a dataframe in Pandas based on multiple columns. import modules. import pandas as pd import numpy as np. create dummy dataframe. raw_data� One way of sorting data in R is to determine the order that elements should be in, if you were to sort. This sounds long winded, but as you’ll see, having this flexibility means you can write statements that are very natural. How to get the order First, determine the element order to sort state.info$Population in ascending order.

How to Sort a Data Frame by Multiple Columns in R, Sort a data frame by multiple columns in R with the order function by vector name , column index or multiple columns. pandas.DataFrame.sort_values¶ DataFrame.sort_values (by, axis = 0, ascending = True, inplace = False, kind = 'quicksort', na_position = 'last', ignore_index = False, key = None) [source] ¶ Sort by the values along either axis. Parameters by str or list of str. Name or list of names to sort by. if axis is 0 or ‘index’ then by may contain index levels and/or column labels.

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!