Pandas Series: sort_values() function
Sort Pandas series in ascending or descending order by some criterion
The sort_values() function is used to sort by the values.
Sort a Series in ascending or descending order by some condition.
Syntax:
Series.sort_values(self, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
axis | Axis to direct sorting. The value ‘index’ is accepted for compatibility with DataFrame.sort_values. | {0 or ‘index’} Default Value: 0 |
Required |
ascending | If True, sort values in ascending order, otherwise descending. | bool Default Value: True |
Required |
inplace | Sort ascending vs. descending. | bool Default Value: True |
Required |
inplace | If True, perform operation in-place. | bool Default Value: False |
Required |
kind | Choice of sorting algorithm. See also numpy.sort() for more information. ‘mergesort’ is the only stable algorithm. | {‘quicksort’, ‘mergesort’ or ‘heapsort’} Default Value: ‘quicksort’ |
Required |
na_position | Argument ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end. | {‘first’ or ‘last’} Default Value: ‘last’ |
Required |
Returns: Series - Series ordered by values.
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([np.nan, 2, 4, 10, 7])
s
Output:
0 NaN 1 2.0 2 4.0 3 10.0 4 7.0 dtype: float64
Example - Sort values ascending order (default behaviour):
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([np.nan, 2, 4, 10, 7])
s.sort_values(ascending=True)
Output:
1 2.0 2 4.0 4 7.0 3 10.0 0 NaN dtype: float64
Example - Sort values descending order:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([np.nan, 2, 4, 10, 7])
s.sort_values(ascending=False)
Output:
3 10.0 4 7.0 2 4.0 1 2.0 0 NaN dtype: float64
Example - Sort values inplace:
Python-Pandas Code:
import numpy as np
import pandas as pd
s.sort_values(ascending=False, inplace=True)
s
Output:
3 10.0 4 7.0 2 4.0 1 2.0 0 NaN dtype: float64
Example - Sort values putting NAs first:
Python-Pandas Code:
import numpy as np
import pandas as pd
s.sort_values(ascending=False, inplace=True)
s.sort_values(na_position='first')
Output:
0 NaN 1 2.0 2 4.0 4 7.0 3 10.0 dtype: float64
Example - Sort a series of strings:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['t', 'q', 's', 'p', 'r'])
s
Output:
0 t 1 q 2 s 3 p 4 r dtype: object
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['t', 'q', 's', 'p', 'r'])
s.sort_values()
Output:
3 p 1 q 4 r 2 s 0 t dtype: object
Previous: Fill NA/missing values in a Pandas series
Next: Sorts Pandas series by labels along the given axis
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/series/series-sort_values.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics