Updated April 1, 2023

## Introduction to Pandas DataFrame.sort()

The following article provides an outline for Pandas DataFrame.sort(). The process of sorting is a very critical concept in any programming language. There will always be a predominant need for sorting the data processed irrespective of the programming language used. The same applies to python pandas library, the sort_values()method in pandas library offers the capability to sort the values of the pandas data structure in most flexible manner and the outcomes of the sort can be retrieved and taken for further processing optimized.

### Syntax and Parameters

Following are the syntax and parameters of pandas dataframe.sort():

**Syntax:**

`DataFrame.sort(column=None, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')`

**Parameters description of above syntax:**

Parameter |
Description |

column |
This argument represents the column name value, so if a column name needs to be mentioned it can be mentioned here. Mentioning a column name in this argument means the dataframe will be sorted based on this column name value. Here the column name can be mentioned as a list or even as a tuple, so the list or tuple of column names based on which the sort is expected to happen will be mentioned as a sequence of entries. In other means this column can be represented as ‘by’ column which means by value through which the sort is expected to happen. |

axis |
This argument represents the column or the axis upon which the sort function needs to be applied on. The value specified in this argument represents either a column, position or a location in a dataframe. To achieve this capability to flexibly travel over a dataframe the axis value is framed on below means,{index (0), columns (1)}. Here mentioning the value of 0 to axis argument gives the sorted value for each and every row in the dataframe, whereas mentioning the value of 1 in the dataframe gives the sort value for all the columns in the dataframe. |

ascending |
Basically sort can be achieved through multiple techniques and each of these techniques have their own capability. So the sorting perception can be applied with two different expected outcomes, that is whether the sort has to happen from low to high or the sort has to happen from high to low. So low to high means ascending were all the outcome values will start from the lowest value and progress towards the highest one. On the other hand high to low is a technique which applies sorting from the highest values to the least ones and this is called descending. This argument is used to determine whether the sorting needs to be ascending or of descending type. Here this argument is depicted as a boolean value which means the argument can be requested for the type of sort to happen through true or false values. Here true stands for ascending and it is the default value. If the sorting is expected to happen in descending then the value can be set to boolean false. |

inplace |
This is used to determine whether the operation needs to be performed at the place of the data. So this means whether the outcome of the sort need to be holded on to the current dataframe for which it is applied. This is again a boolean variable, if this is set to true then the sorted changes will be applied to the current dataframe itself, if this argument is assigned as false then no changes will be applied to the current dataframe. A equal relation can be used to pull the updated sorted dataframe into a different dataframe. |

kind |
This is a very important argument, this represents what kind of technique needs to be used for the sorting. There are several techniques or ways through which sorting can be achieved and this argument assignates it. This argument can hold three different values {‘quicksort’, ‘mergesort’, ‘heapsort’} and quicksort is the default value of this argument. From a dataframe perspective this argument is applicable only for sorting of single columns. |

na_position |
The na_position argument is used to decide whether the NaN’s have to be placed at the beginning or at the last. |

### Examples of Pandas DataFrame.sort()

Given below are the examples of Pandas DataFrame.sort():

#### Example #1

**Code:**

```
import pandas as pd
Core_Series = pd.Series([ 20, 70, 10, 40, 50, 60])
print(" THE CORE SERIES ")
print(Core_Series)
Sorted_series_descending = Core_Series.sort_values(ascending=False)
print("")
print(" THE SORTED SERIES:\n",Sorted_series_descending)
```

**Output: **

**Explanation:**

- Here the pandas library is initially imported and the imported library is used for creating a series.
- The values in the series are formulated in such way that they are a series of 10 to 60 but the values in this series are in jumbled manner without applying to any specific order. The sort() method is used for determining the sorted series and print it on to the console.
- Setting the ascending argument to false makes the sorting to happen in descending order for the series and printed on to the console.

#### Example #2

**Code:**

```
import pandas as pd
Core_Dataframe = pd.DataFrame({'A' : [ 11, 6, 11, 45, 21, 26],
'B' : [23, 27, 12, 17, 72, 27],
'C' : [34, 8, 13, 148, 83, 28],
'D' : [14, 9, 4, 19, 24, 29],
'E' : [5, 10, 15, 20, 25, 30]})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe,'\n')
Core_Dataframe_sorted_inplace = Core_Dataframe.sort_values(by=['A','B','C','D','E'],inplace=True)
print(" UPDATED DATAFRAME:",Core_Dataframe_sorted_inplace,'\n')
print(" CORE DATAFRAME SORTED ASCENDING: \n",Core_Dataframe,'\n')
Core_Dataframe.sort_values(by=['A','B','C','D','E'],inplace=True,ascending=False)
print(" CORE DATAFRAME SORTED DESCENDING: \n",Core_Dataframe)
```

**Output: **

**Explanation:**

- Here the pandas library is initially imported and the imported library is used for creating the dataframe which is a shape(6,6).
- All of the columns in the dataframe are assigned with headers which are alphabetic. The values in the dataframe are formulated in such way that they are of a jumbled series of 1 to n.
- Here the data frame created is mentioned as core dataframe. The dataframe is sorted with a inplace = true , first it is sorted in ascending order and then it is sorted in descending order and both the outcomes are printed on to the console.

### Recommended Articles

We hope that this EDUCBA information on “Pandas DataFrame.sort()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.