pandas 1.4.2

ParametersReturnsBackRef
sort_index(self, axis=0, level=None, ascending: 'bool | int | Sequence[bool | int]' = True, inplace: 'bool' = False, kind: 'str' = 'quicksort', na_position: 'str' = 'last', sort_remaining: 'bool' = True, ignore_index: 'bool' = False, key: 'IndexKeyFunc' = None)

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

Parameters

axis : int, default 0

Axis to direct sorting. This can only be 0 for Series.

level : int, optional

If not None, sort on values in specified index level(s).

ascending : bool or list-like of bools, default True

Sort ascending vs. descending. When the index is a MultiIndex the sort direction can be controlled for each level individually.

inplace : bool, default False

If True, perform operation in-place.

kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort'

Choice of sorting algorithm. See also numpy.sort for more information. 'mergesort' and 'stable' are the only stable algorithms. For DataFrames, this option is only applied when sorting on a single column or label.

na_position : {'first', 'last'}, default 'last'

If 'first' puts NaNs at the beginning, 'last' puts NaNs at the end. Not implemented for MultiIndex.

sort_remaining : bool, default True

If True and sorting by level and index is multilevel, sort by other levels too (in order) after sorting by specified level.

ignore_index : bool, default False

If True, the resulting axis will be labeled 0, 1, …, n - 1.

versionadded
key : callable, optional

If not None, apply the key function to the index values before sorting. This is similar to the :None:None:`key` argument in the builtin sorted function, with the notable difference that this :None:None:`key` function should be vectorized. It should expect an Index and return an Index of the same shape.

versionadded

Returns

Series or None

The original Series sorted by the labels or None if inplace=True .

Sort Series by index labels.

See Also

DataFrame.sort_index

Sort DataFrame by the index.

DataFrame.sort_values

Sort DataFrame by the value.

Series.sort_values

Sort Series by the value.

Examples

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(['a', 'b', 'c', 'd'], index=[3, 2, 1, 4])
... s.sort_index() 1 c 2 b 3 a 4 d dtype: object

Sort Descending

This example is valid syntax, but we were not able to check execution
>>> s.sort_index(ascending=False)
4    d
3    a
2    b
1    c
dtype: object

Sort Inplace

This example is valid syntax, but we were not able to check execution
>>> s.sort_index(inplace=True)
... s 1 c 2 b 3 a 4 d dtype: object

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

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(['a', 'b', 'c', 'd'], index=[3, 2, 1, np.nan])
... s.sort_index(na_position='first') NaN d 1.0 c 2.0 b 3.0 a dtype: object

Specify index level to sort

This example is valid syntax, but we were not able to check execution
>>> arrays = [np.array(['qux', 'qux', 'foo', 'foo',
...  'baz', 'baz', 'bar', 'bar']),
...  np.array(['two', 'one', 'two', 'one',
...  'two', 'one', 'two', 'one'])]
... s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=arrays)
... s.sort_index(level=1) bar one 8 baz one 6 foo one 4 qux one 2 bar two 7 baz two 5 foo two 3 qux two 1 dtype: int64

Does not sort by remaining levels when sorting by levels

This example is valid syntax, but we were not able to check execution
>>> s.sort_index(level=1, sort_remaining=False)
qux  one    2
foo  one    4
baz  one    6
bar  one    8
qux  two    1
foo  two    3
baz  two    5
bar  two    7
dtype: int64

Apply a key function before sorting

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([1, 2, 3, 4], index=['A', 'b', 'C', 'd'])
... s.sort_index(key=lambda x : x.str.lower()) A 1 b 2 C 3 d 4 dtype: int64
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.frame.DataFrame.sort_index pandas.core.series.Series.sort_values

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/series.py#3540
type: <class 'function'>
Commit: