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:


Download the above Notebook from here.



Inviting useful, relevant, well-written and unique guest posts