Profile picture

Pandas Series - Sorting

Last updated: October 31st, 20192019-10-31Project preview

rmotr


Pandas Series - Sorting

In many use cases Series values need to be sorted.

Sorting in Pandas is extremely easy. There are two important methods to be used for Series and DataFrames that will take care of the job: sort_values and sort_index.

purple-divider

Hands on!

In [1]:
import pandas as pd
import numpy as np
In [2]:
pd.options.display.float_format = '{:,.2f}'.format

green-divider

The first thing we'll do is create again the Series from our previous lecture:

In [3]:
g7_pop = pd.Series({
    'Canada': 35.467,
    'France': 63.951,
    'Germany': 80.94,
    'Italy': 60.665,
    'Japan': 127.061,
    'United Kingdom': 64.511,
    'United States': 318.523
}, dtype=np.float, name='G7 Population in millions')
In [4]:
g7_pop
Out[4]:
Canada            35.47
France            63.95
Germany           80.94
Italy             60.66
Japan            127.06
United Kingdom    64.51
United States    318.52
Name: G7 Population in millions, dtype: float64
In [5]:
gdp = pd.Series(
    [1785387, 2833687, 3874437, 2167744, 4602367, 2950039, 17348075],
    index=['Canada', 'France', 'Germany', 'Italy',
            'Japan', 'United Kingdom', 'United States'],
    dtype=np.float,
    name='G7 GDP in millions')

green-divider

 Sorting values

In [6]:
g7_pop
Out[6]:
Canada            35.47
France            63.95
Germany           80.94
Italy             60.66
Japan            127.06
United Kingdom    64.51
United States    318.52
Name: G7 Population in millions, dtype: float64
In [7]:
g7_pop.sort_values()
Out[7]:
Canada            35.47
Italy             60.66
France            63.95
United Kingdom    64.51
Germany           80.94
Japan            127.06
United States    318.52
Name: G7 Population in millions, dtype: float64

As you can see, sorting is as simple as invoking the sort_values method. By default, values are sorted in ascending order, which you can customize with the ascending parameter.

In [8]:
g7_pop.sort_values(ascending=False)
Out[8]:
United States    318.52
Japan            127.06
Germany           80.94
United Kingdom    64.51
France            63.95
Italy             60.66
Canada            35.47
Name: G7 Population in millions, dtype: float64
In [9]:
g7_pop
Out[9]:
Canada            35.47
France            63.95
Germany           80.94
Italy             60.66
Japan            127.06
United Kingdom    64.51
United States    318.52
Name: G7 Population in millions, dtype: float64
In [10]:
g7_pop.sort_values(ascending=False, inplace=True)
In [11]:
g7_pop
Out[11]:
United States    318.52
Japan            127.06
Germany           80.94
United Kingdom    64.51
France            63.95
Italy             60.66
Canada            35.47
Name: G7 Population in millions, dtype: float64

Sorting index

sort_index works exactly in the same way:

In [12]:
g7_pop.sort_index()
Out[12]:
Canada            35.47
France            63.95
Germany           80.94
Italy             60.66
Japan            127.06
United Kingdom    64.51
United States    318.52
Name: G7 Population in millions, dtype: float64

purple-divider

Notebooks AI
Notebooks AI Profile20060