pandas 1.4.2

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

Sort a Series in ascending or descending order by some criterion.

Parameters

axis : {0 or 'index'}, default 0

Axis to direct sorting. The value 'index' is accepted for compatibility with DataFrame.sort_values.

ascending : bool or list of bools, default True

If True, sort values in ascending order, otherwise descending.

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.

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

Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at the end.

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 series 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 a Series and return an array-like.

versionadded

Returns

Series or None

Series ordered by values or None if inplace=True .

Sort by the values.

See Also

DataFrame.sort_index

Sort DataFrame by indices.

DataFrame.sort_values

Sort DataFrame by the values along either axis.

Series.sort_index

Sort by the Series indices.

Examples

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([np.nan, 1, 3, 10, 5])
... s 0 NaN 1 1.0 2 3.0 3 10.0 4 5.0 dtype: float64

Sort values ascending order (default behaviour)

This example is valid syntax, but we were not able to check execution
>>> s.sort_values(ascending=True)
1     1.0
2     3.0
4     5.0
3    10.0
0     NaN
dtype: float64

Sort values descending order

This example is valid syntax, but we were not able to check execution
>>> s.sort_values(ascending=False)
3    10.0
4     5.0
2     3.0
1     1.0
0     NaN
dtype: float64

Sort values inplace

This example is valid syntax, but we were not able to check execution
>>> s.sort_values(ascending=False, inplace=True)
... s 3 10.0 4 5.0 2 3.0 1 1.0 0 NaN dtype: float64

Sort values putting NAs first

This example is valid syntax, but we were not able to check execution
>>> s.sort_values(na_position='first')
0     NaN
1     1.0
2     3.0
4     5.0
3    10.0
dtype: float64

Sort a series of strings

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

Sort using a key function. Your :None:None:`key` function will be given the Series of values and should return an array-like.

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(['a', 'B', 'c', 'D', 'e'])
... s.sort_values() 1 B 3 D 0 a 2 c 4 e dtype: object
This example is valid syntax, but we were not able to check execution
>>> s.sort_values(key=lambda x: x.str.lower())
0    a
1    B
2    c
3    D
4    e
dtype: object

NumPy ufuncs work well here. For example, we can sort by the sin of the value

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([-4, -2, 0, 2, 4])
... s.sort_values(key=np.sin) 1 -2 4 4 2 0 0 -4 3 2 dtype: int64

More complicated user-defined functions can be used, as long as they expect a Series and return an array-like

This example is valid syntax, but we were not able to check execution
>>> s.sort_values(key=lambda x: (np.tan(x.cumsum())))
0   -4
3    2
4    4
1   -2
2    0
dtype: int64
See :

Back References

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

pandas.core.indexes.base.Index.sort_values pandas.core.series.Series.sort_index pandas.core.frame.DataFrame.sort_values pandas.core.arrays.categorical.Categorical.sort_values pandas.core.series.Series.nlargest pandas.core.generic.NDFrame.sort_values pandas.core.groupby.generic.SeriesGroupBy.nsmallest pandas.core.series.Series.searchsorted pandas.core.groupby.generic.SeriesGroupBy.nlargest pandas.core.series.Series.nsmallest pandas.core.frame.DataFrame.sort_index

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#3330
type: <class 'function'>
Commit: