Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.Series.sort_values#

Series.sort_values(*,axis=0,ascending=True,inplace=False,kind='quicksort',na_position='last',ignore_index=False,key=None)[source]#

Sort by the values.

Sort a Series in ascending or descending order by somecriterion.

Parameters:
axis{0 or ‘index’}

Unused. Parameter needed for compatibility with DataFrame.

ascendingbool or list of bools, default True

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

inplacebool, default False

If True, perform operation in-place.

kind{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, default ‘quicksort’

Choice of sorting algorithm. See alsonumpy.sort() for moreinformation. ‘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 atthe end.

ignore_indexbool, default False

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

keycallable, optional

If not None, apply the key function to the series valuesbefore sorting. This is similar to thekey argument in thebuiltinsorted() function, with the notable difference thatthiskey function should bevectorized. It should expect aSeries and return an array-like.

Returns:
Series or None

Series ordered by values or None ifinplace=True.

See also

Series.sort_index

Sort by the Series indices.

DataFrame.sort_values

Sort DataFrame by the values along either axis.

DataFrame.sort_index

Sort DataFrame by indices.

Examples

>>>s=pd.Series([np.nan,1,3,10,5])>>>s0     NaN1     1.02     3.03     10.04     5.0dtype: float64

Sort values ascending order (default behaviour)

>>>s.sort_values(ascending=True)1     1.02     3.04     5.03    10.00     NaNdtype: float64

Sort values descending order

>>>s.sort_values(ascending=False)3    10.04     5.02     3.01     1.00     NaNdtype: float64

Sort values putting NAs first

>>>s.sort_values(na_position='first')0     NaN1     1.02     3.04     5.03    10.0dtype: float64

Sort a series of strings

>>>s=pd.Series(['z','b','d','a','c'])>>>s0    z1    b2    d3    a4    cdtype: object
>>>s.sort_values()3    a1    b4    c2    d0    zdtype: object

Sort using a key function. Yourkey function will begiven theSeries of values and should return an array-like.

>>>s=pd.Series(['a','B','c','D','e'])>>>s.sort_values()1    B3    D0    a2    c4    edtype: object>>>s.sort_values(key=lambdax:x.str.lower())0    a1    B2    c3    D4    edtype: object

NumPy ufuncs work well here. For example, we cansort by thesin of the value

>>>s=pd.Series([-4,-2,0,2,4])>>>s.sort_values(key=np.sin)1   -24    42    00   -43    2dtype: int64

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

>>>s.sort_values(key=lambdax:(np.tan(x.cumsum())))0   -43    24    41   -22    0dtype: int64

[8]ページ先頭

©2009-2025 Movatter.jp