plydata.cat_tools.cat_other

plydata.cat_tools.cat_other(c, keep=None, drop=None, other_category='other')[source]

Replace categories with 'other'

Parameters
clist-like

Values that will make up the categorical.

keeplist-like

Categories to preserve. Only one of keep or drop should be specified.

droplist-like

Categories to drop. Only one of keep or drop should be specified.

other_categoryobject

Value used for the 'other' values. It is placed at the end of the categories.

Returns
outcategorical

Values

Examples

>>> c = ['a', 'b', 'a', 'c', 'b', 'b', 'b', 'd', 'c']
>>> cat_other(c, keep=['a', 'b'])
['a', 'b', 'a', 'other', 'b', 'b', 'b', 'other', 'other']
Categories (3, object): ['a', 'b', 'other']
>>> cat_other(c, drop=['a', 'b'])
['other', 'other', 'other', 'c', 'other', 'other', 'other', 'd', 'c']
Categories (3, object): ['c', 'd', 'other']
>>> cat_other(pd.Categorical(c, ordered=True), drop=['a', 'b'])
['other', 'other', 'other', 'c', 'other', 'other', 'other', 'd', 'c']
Categories (3, object): ['c' < 'd' < 'other']