AppDividend
Latest Code Tutorials

Pandas Series Sort_values Tutorial With Example

0

Pandas Series Sort_values Tutorial With Example is today’s topic. Pandas sort_values() function 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 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 Series sort_values() Tutorial With 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.

https://docs.google.com/spreadsheets/d/1zeeZQzFoHE2j_ZrqDkVJK9eF7OH1yvg75c8S-aBcxaU/edit#gid=0

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 DataFrame data structure in Pandas. So write the following code in the next cell.

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

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

Next step is to use a sort_values() function on the any one of columns.

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

Let’s sort the city based on city name.

Write the following code in the next cell.

data.City.sort_values()

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'])
dataFSort

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.

# app.py

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

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

See the output below in the terminal.

 

Pandas DataFrame sort_values() Example Tutorial

Finally, Pandas Series Sort_values Tutorial With 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.