pandas 1.4.2

ParametersReturnsBackRef
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'.

Parameters

inplace : bool, default False

Do operation in place.

ascending : bool, default True

Order ascending. Passing False orders descending. The ordering parameter provides the method by which the category values are organized.

na_position : {'first', 'last'} (optional, default='last')

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

Returns

Categorical or None

Sort the Categorical by category value returning a new Categorical by default.

See Also

Categorical.sort
Series.sort_values

Examples

This example is valid syntax, but we were not able to check execution
>>> c = pd.Categorical([1, 2, 2, 1, 5])
... c [1, 2, 2, 1, 5] Categories (3, int64): [1, 2, 5]
This example is valid syntax, but we were not able to check execution
>>> 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)
... c [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 = 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])
... c [NaN, 2, 2, NaN, 5] Categories (2, int64): [2, 5]
This example is valid syntax, but we were not able to check execution
>>> 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 :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.arrays.categorical.Categorical.sort_values

Local connectivity graph

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


File: /pandas/core/arrays/categorical.py#1766
type: <class 'function'>
Commit: