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.


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


Name Description Type/Default Value Required / Optional
axis Axis to direct sorting. This can only be 0 for Series. int
Default Value: 0
level If not None, sort on values in specified index level(s). int
ascending Sort ascending vs. descending. bool
Default Value: True
inplace If True, perform operation in-place. bool
Default Value: False
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’
na_position If ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end. Not implemented for MultiIndex. {‘first’, ‘last’}
Default Value: ‘last’
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

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


Download the above Notebook from here.

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