Sort by the values.
Sort a Series in ascending or descending order by some criterion.
Unused. Parameter needed for compatibility with DataFrame.
If True, sort values in ascending order, otherwise descending.
If True, perform operation in-place.
Choice of sorting algorithm. See also numpy.sort()
for more information. âmergesortâ and âstableâ are the only stable algorithms.
Argument âfirstâ puts NaNs at the beginning, âlastâ puts NaNs at the end.
If True, the resulting axis will be labeled 0, 1, â¦, n - 1.
If not None, apply the key function to the series values before sorting. This is similar to the key argument in the builtin sorted()
function, with the notable difference that this key function should be vectorized. It should expect a Series
and return an array-like.
Series ordered by values or None if inplace=True
.
Examples
>>> 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)
>>> 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 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 = pd.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
Sort using a key function. Your key function will be given the Series
of values and should return an array-like.
>>> s = pd.Series(['a', 'B', 'c', 'D', 'e']) >>> s.sort_values() 1 B 3 D 0 a 2 c 4 e dtype: object >>> 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
>>> 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
>>> s.sort_values(key=lambda x: (np.tan(x.cumsum()))) 0 -4 3 2 4 4 1 -2 2 0 dtype: int64
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4