Create categorical codes
Convert repeated labels such as categories, names, or tokens into compact integer representations for downstream array operations.
Pandas · Function
pandas.factorize encodes a one-dimensional sequence as integer codes and returns the corresponding unique values. By default, missing values receive -1 and are omitted from uniques.
Pass a one-dimensional sequence to receive two aligned results: integer codes and the distinct valid values they index. By default, distinct values retain discovery order, while missing values are represented by -1 and omitted from the uniques result; sorting or explicit missing-value encoding changes those behaviors.
pandas.factorize(values, sort=False, use_na_sentinel=True, size_hint=None)| Name | Type | Description |
|---|---|---|
values | sequence | A one-dimensional sequence. Non-pandas sequences are coerced to ndarrays. |
sort | bool, default False | Whether to sort unique values and remap codes to preserve their relationship; default is False. |
use_na_sentinel | bool, default True | Whether missing values use sentinel -1; default is True. If False, missing values receive non-negative codes and remain in uniques. |
size_hint | int, optional | Optional hint for the hashtable sizer. |
A tuple of codes and uniques. codes is an integer ndarray indexing uniques. uniques contains the unique valid values and is a Categorical for Categorical input, an Index for other pandas objects, or a one-dimensional ndarray otherwise.
import pandas as pd
values = ["cat", "dog", "cat", None]
codes, uniques = pd.factorize(values)
print(codes) # [ 0 1 0 -1]
print(uniques) # ['cat' 'dog']The returned integer codes identify each value's position in the unique-values array; the missing value receives the default sentinel code -1.
Convert repeated labels such as categories, names, or tokens into compact integer representations for downstream array operations.
Obtain both an encoded array and the distinct values that define its codebook.
Use stable encounter-order labels or sorted labels depending on whether ordering or deterministic ranking is required.
It returns a pair: an integer ndarray of codes and the unique valid values. Each code indexes into uniques, so uniques.take(codes) reconstructs the input values where codes are valid.
Follow-up: What happens with missing values? By default they map to -1 and are excluded from uniques; use_na_sentinel=False instead assigns them a non-negative code and retains them.
The default preserves the order in which distinct values are encountered. With sort=True, pandas sorts the unique values and adjusts the codes so they still refer to the corresponding values.
Follow-up: Why must codes be shuffled after sorting? Because each code is an index into the reordered uniques array, so unchanged codes would identify the wrong values.
The input is a one-dimensional sequence, and non-pandas sequences are converted to ndarrays before factorization. The type of uniques depends on the input: categoricals produce a Categorical, other pandas objects produce an Index, and other inputs produce a one-dimensional ndarray.
Follow-up: How would you preserve an input-specific unique-value type? Pass the relevant pandas object or Categorical rather than first converting it to a plain array.
pandas.Series.factorizepandas.Index.factorize