plydata.cat_tools.cat_reorder

plydata.cat_tools.cat_reorder(c, x, fun=<function median>, ascending=True)[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.

funcallable()

Summarising function to x for each category in c. Default is the median.

ascendingbool

If True, the c is ordered in ascending order of x.

Examples

>>> c = list('abbccc')
>>> x = [11, 2, 2, 3, 33, 3]
>>> cat_reorder(c, x)
['a', 'b', 'b', 'c', 'c', 'c']
Categories (3, object): ['b', 'c', 'a']
>>> cat_reorder(c, x, fun=max)
['a', 'b', 'b', 'c', 'c', 'c']
Categories (3, object): ['b', 'a', 'c']
>>> cat_reorder(c, x, fun=max, ascending=False)
['a', 'b', 'b', 'c', 'c', 'c']
Categories (3, object): ['c', 'a', 'b']
>>> c_ordered = pd.Categorical(c, ordered=True)
>>> cat_reorder(c_ordered, x)
['a', 'b', 'b', 'c', 'c', 'c']
Categories (3, object): ['b' < 'c' < 'a']
>>> cat_reorder(c + ['d'], x)
Traceback (most recent call last):
    ...
ValueError: Lengths are not equal. len(c) is 7 and len(x) is 6.