plydata.cat_tools.cat_reorder2¶
-
plydata.cat_tools.
cat_reorder2
(c, x, y, *args, fun=<function last2>, ascending=False, **kwargs)[source]¶ Reorder categorical by sorting along another variable
It is the order of the categories that changes. Values in x are grouped by categories and summarised to determine the new order.
- Parameters
- clist-like
Values that will make up the categorical.
- xlist-like
Values by which
c
will be ordered.- ylist-like
Values by which
c
will be ordered.- *args
tuple
Position arguments passed to function fun.
- fun
callable()
Summarising function to
x
for each category inc
. Default is the median.- ascendingbool
If
True
, thec
is ordered in ascending order ofx
.- **kwargs
dict
Keyword arguments passed to
fun
.
Examples
Order stocks by the price in the latest year. This type of ordering can be used to order line plots so that the ends match the order of the legend.
>>> stocks = list('AAABBBCCC') >>> year = [1980, 1990, 2000] * 3 >>> price = [12.34, 12.90, 13.55, 10.92, 14.73, 11.08, 9.02, 12.44, 15.65] >>> cat_reorder2(stocks, year, price) ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'] Categories (3, object): ['C', 'A', 'B']