Latest Code Tutorials

Pandas Sort_values Example | Pandas Dataframe.sort_values()


Pandas sort_values() is an inbuilt series function that sorts the data frame in Ascending or Descending order of provided column. It is different than the sorted Python function since it cannot sort a data frame and a particular column cannot be selected. If you want to sort a Series in ascending or descending order by some criteria then the Pandas sort_values() method is useful.

Pandas sort_values() Example

Let’s see the syntax for a value_counts method in Python Pandas Library.

Series.sort_values(axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')

The description of the parameters is the following.

axis : {0 or ‘index’}, default 0.

Axis to the direct sorting. A value ‘index’ is accepted for compatibility with DataFrame.sort_values.

ascending: bool and default True

If True, sort values in ascending order, otherwise descending.

inplace: bool and default False

If True then perform operation in-place.

kind: {‘quicksort’, ‘mergesort’ or ‘heapsort’}, default ‘quicksort’

Choice of sorting algorithm. See also numpy.sort() for more information. ‘mergesort’ is an only stable algorithm.

na_position: {‘first’ or ‘last’}, default ‘last’.

Pandas Series sort_values() Example

Okay, now for this tutorial, we will use the Jupyter Notebook. Also, we need data to work on this project. You can save the CSV file from the below URL.

Now, open the Jupyter Notebook and import the Pandas Library first.

Write the following code inside the first cell in Jupyter Notebook.

import pandas as pd

Run the cell by hitting Ctrl + Enter.

Okay, now we will use the  read_csv()function of the DataFrame data structure in Pandas.

So write the following code in the next cell.

data = pd.read_csv('data.csv', skiprows=4)

So, we have used the read_csv() function and skipped the first four rows and then display the remaining rows. Run the cell and see the output.

Pandas Series sort_values() Example

The next step is to use a sort_values() function on any one of columns.

Remember, here we are using sort_values() function using with Series. There is also a sort_values() method available for DataFrame as well.

Let’s sort the city based on the city name.

Write the following code in the next cell.


Now, see the output below.


Pandas Series sort_values() Tutorial With Example

So, it has sorted based on the name of the cities. In the above case, the cities are from Amsterdam to Tokyo.

Pandas DataFrame sort_values() Example

Now, we are sorting the DataFrame based on the multiple series provided in the Parameters.

Write the following code inside a new cell.

dataFSort = data.sort_values(['City', 'Sport'])

So, in the above code, what we have done is that we have sorted the DataFrame based on two columns and that is City and Sport. The output of the above code is following.

Pandas DataFrame sort_values() Example

So, in this tutorial, we have seen the sort_values() on Series and DataFrames.

If you are not using Jupyter Notebook, then you can write the following code in the editor and see the output inside the terminal.


import pandas as pd
data = pd.read_csv('data.csv', skiprows=4)

print(data.sort_values(['City', 'Sport']))

See the output below in the terminal.

Pandas DataFrame sort_values() Example Tutorial

Finally, Pandas sort_values example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.