sort_values(self, inplace: 'bool' = False, ascending: 'bool' = True, na_position: 'str' = 'last')
While an ordering is applied to the category values, sorting in this context refers more to organizing and grouping together based on matching category values. Thus, this function can be called on an unordered Categorical instance unlike the functions 'Categorical.min' and 'Categorical.max'.
Do operation in place.
Order ascending. Passing False orders descending. The ordering parameter provides the method by which the category values are organized.
'first' puts NaNs at the beginning 'last' puts NaNs at the end
Sort the Categorical by category value returning a new Categorical by default.
>>> c = pd.Categorical([1, 2, 2, 1, 5])This example is valid syntax, but we were not able to check execution
... c [1, 2, 2, 1, 5] Categories (3, int64): [1, 2, 5]
>>> c.sort_values() [1, 1, 2, 2, 5] Categories (3, int64): [1, 2, 5]This example is valid syntax, but we were not able to check execution
>>> c.sort_values(ascending=False) [5, 2, 2, 1, 1] Categories (3, int64): [1, 2, 5]
Inplace sorting can be done as well:
This example is valid syntax, but we were not able to check execution>>> c.sort_values(inplace=True)This example is valid syntax, but we were not able to check execution
... c [1, 1, 2, 2, 5] Categories (3, int64): [1, 2, 5] >>>
>>> c = pd.Categorical([1, 2, 2, 1, 5])
'sort_values' behaviour with NaNs. Note that 'na_position' is independent of the 'ascending' parameter:
This example is valid syntax, but we were not able to check execution>>> c = pd.Categorical([np.nan, 2, 2, np.nan, 5])This example is valid syntax, but we were not able to check execution
... c [NaN, 2, 2, NaN, 5] Categories (2, int64): [2, 5]
>>> c.sort_values() [2, 2, 5, NaN, NaN] Categories (2, int64): [2, 5]This example is valid syntax, but we were not able to check execution
>>> c.sort_values(ascending=False) [5, 2, 2, NaN, NaN] Categories (2, int64): [2, 5]This example is valid syntax, but we were not able to check execution
>>> c.sort_values(na_position='first') [NaN, NaN, 2, 2, 5] Categories (2, int64): [2, 5]This example is valid syntax, but we were not able to check execution
>>> c.sort_values(ascending=False, na_position='first') [NaN, NaN, 5, 2, 2] Categories (2, int64): [2, 5]See :
The following pages refer to to this document either explicitly or contain code examples using this.
pandas.core.arrays.categorical.Categorical.sort_values
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