w3resource

Pandas Series: sort_index() function

Sorts Pandas series by labels along the given axis

The sort_index() function is used to sort Series by index labels.

Returns a new Series sorted by label if inplace argument is False, otherwise updates the original series and returns None.

Syntax:

Series.sort_index(self, axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True)
Pandas Series sort_index image

Parameters:

Name Description Type/Default Value Required / Optional
axis Axis to direct sorting. This can only be 0 for Series. int
Default Value: 0
Required
level If not None, sort on values in specified index level(s). int
Optional
ascending Sort ascending vs. descending. bool
Default Value: True
Required
inplace If True, perform operation in-place. bool
Default Value: False
Required
kind Choice of sorting algorithm. See also numpy.sort() for more information. ‘mergesort’ is the only stable algorithm. For DataFrames, this option is only applied when sorting on a single column or label. {‘quicksort’, ‘mergesort’, ‘heapsort’}
Default Value: 'quicksort’
Required
na_position If ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end. Not implemented for MultiIndex. {‘first’, ‘last’}
Default Value: ‘last’
Required
sort_remaining If True and sorting by level and index is multilevel, sort by other levels too (in order) after sorting by specified level. bool
Default Value: True
Required

Returns: Series- The original Series sorted by the labels.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['p', 'q', 'r', 's'], index=[3, 2, 4, 5])
s.sort_index()

Output:

2    q
3    p
4    r
5    s
dtype: object
Pandas Series sort_index image

Example - Sort Descending:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['p', 'q', 'r', 's'], index=[3, 2, 4, 5])
s.sort_index(ascending=False)

Output:

5    s
4    r
3    p
2    q
dtype: object

Example - Sort Inplace:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['p', 'q', 'r', 's'], index=[3, 2, 4, 5])
s.sort_index(inplace=True)
s

Output:

2    q
3    p
4    r
5    s
dtype: object

Example - By default NaNs are put at the end, but use na_position to place them at the beginning:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['p', 'q', 'r', 's'], index=[3, 2, 4, np.nan])
s.sort_index(na_position='first')

Output:

NaN    s
2.0    q
3.0    p
4.0    r
dtype: object

Example - Specify index level to sort:

Python-Pandas Code:

import numpy as np
import pandas as pd
arrays = [np.array(['xx', 'xx', 'ff', 'ff',
                    'bb', 'bb', 'br', 'br']),
          np.array(['two', 'one', 'two', 'one',
                    'two', 'one', 'two', 'one'])]
s = pd.Series([2, 3, 4, 5, 6, 7, 8, 9], index=arrays)
s.sort_index(level=1)

Output:

bb  one    7
br  one    9
ff  one    5
xx  one    3
bb  two    6
br  two    8
ff  two    4
xx  two    2
dtype: int64

Example - Does not sort by remaining levels when sorting by levels:

Python-Pandas Code:

import numpy as np
import pandas as pd
arrays = [np.array(['xx', 'xx', 'ff', 'ff',
                    'bb', 'bb', 'br', 'br']),
          np.array(['two', 'one', 'two', 'one',
                    'two', 'one', 'two', 'one'])]
s = pd.Series([2, 3, 4, 5, 6, 7, 8, 9], index=arrays)
s.sort_index(level=1, sort_remaining=False)

Output:

xx  one    3
ff  one    5
bb  one    7
br  one    9
xx  two    2
ff  two    4
bb  two    6
br  two    8
dtype: int64

Previous: Sort Pandas series in ascending or descending order by some criterion
Next: Series-unstack() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-sort_index.php