plydata.cat_tools.cat_relabel¶
-
plydata.cat_tools.
cat_relabel
(c, func=None, *args, **kwargs)[source]¶ Change/rename categories and collapse as necessary
- Parameters
- clist-like
Values that will make up the categorical.
- func
callable()
Function to create the new name. The first argument to the function will be a category to be renamed.
- *args
tuple
Positional arguments passed to
func
.- *kwargs
dict
Keyword arguments passed to
func
.
Examples
>>> c = list('abcde') >>> cat_relabel(c, str.upper) ['A', 'B', 'C', 'D', 'E'] Categories (5, object): ['A', 'B', 'C', 'D', 'E'] >>> c = pd.Categorical([0, 1, 2, 1, 1, 0]) >>> def func(x): ... if x == 0: ... return 'low' ... elif x == 1: ... return 'mid' ... elif x == 2: ... return 'high' >>> cat_relabel(c, func) ['low', 'mid', 'high', 'mid', 'mid', 'low'] Categories (3, object): ['low', 'mid', 'high']
When the function yields the same output for 2 or more different categories, those categories are collapsed.
>>> def first(x): ... return x[0] >>> c = pd.Categorical(['aA', 'bB', 'aC', 'dD'], ... categories=['bB', 'aA', 'dD', 'aC'], ... ordered=True ... ) >>> cat_relabel(c, first) ['a', 'b', 'a', 'd'] Categories (3, object): ['b' < 'a' < 'd']