pyspark.pandas.Series.sort_values

Series.sort_values(ascending: bool = True, inplace: bool = False, na_position: str = 'last', ignore_index: bool = False) → Optional[pyspark.pandas.series.Series][source]

Sort by the values.

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

Parameters
ascendingbool or list of bool, default True

Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by.

inplacebool, default False

if True, perform operation in-place

na_position{‘first’, ‘last’}, default ‘last’

first puts NaNs at the beginning, last puts NaNs at the end

ignore_indexbool, default False

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

New in version 3.4.0.

Returns
sorted_objSeries ordered by values.

Examples

>>> s = ps.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)

>>> 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

>>> s.sort_values(ascending=False)
3    10.0
4     5.0
2     3.0
1     1.0
0     NaN
dtype: float64

Sort values descending order and ignoring index

>>> s.sort_values(ascending=False, ignore_index=True)
0    10.0
1     5.0
2     3.0
3     1.0
4     NaN
dtype: float64

Sort values inplace

>>> 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

>>> 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

>>> s = ps.Series(['z', 'b', 'd', 'a', 'c'])
>>> s
0    z
1    b
2    d
3    a
4    c
dtype: object
>>> s.sort_values()
3    a
1    b
4    c
2    d
0    z
dtype: object