import pandas as pd
s = pd.Series(['apple', 'banana', 'apple'])
labels, uniques = pd.factorize(s)
print(labels)
Expected Output:
[0 1 0]
The pd.factorize() function encodes 1D categorical structures, text Series, or object arrays into discrete integer codes while simultaneously returning an index of unique categories. In machine learning feature engineering, statistical modeling, and data compression, algorithms require numerical inputs rather than raw string strings. Unlike get_dummies(), which expands categorical features into high-dimensional binary sparse matrices, factorize() preserves 1D array shapes by mapping distinct category strings to sequential integers (e.g., 0, 1, 2...).
pd.factorize(values, sort=False, use_na_sentinel=True)values: 1D sequence, Series, or Categorical array to encode.sort: Boolean (default False). If True, sorts unique values alphabetically before assigning integer codes.use_na_sentinel: Boolean (default True). If True, missing (NaN) values receive a sentinel integer code of -1 and are excluded from the returned unique index.(codes, uniques) where codes is an ndarray of integer codes and uniques is an Index or Categorical array of original values.
"Cat", "Dog", "Bird") to be encoded as integers (0, 1, 2).int8 or int16 codes significantly reduces overall RAM consumption.factorize() returns both integer codes and the unique category index, decoding integer predictions back to original human-readable strings is achieved effortlessly via uniques[codes].By default, factorize() assigns -1 to missing values, preventing NaN entries from creating spurious category classes.
import pandas as pd
import numpy as np
s_nan = pd.Series(['High', np.nan, 'Low', 'High'])
codes, uniques = pd.factorize(s_nan)
print("Codes:", codes)
print("Uniques:", uniques)
Expected Output:
Codes: [ 0 -1 1 0]
Uniques: Index(['High', 'Low'], dtype='object')
Setting sort=True guarantees consistent integer code assignment across different datasets by sorting categories alphabetically prior to encoding.
import pandas as pd
s_dept = pd.Series(['Sales', 'Admin', 'Sales', 'IT'])
codes_sorted, uniques_sorted = pd.factorize(s_dept, sort=True)
print("Sorted Codes:", codes_sorted)
print("Sorted Uniques:", uniques_sorted)
Expected Output:
Sorted Codes: [2 0 2 1]
Sorted Uniques: Index(['Admin', 'IT', 'Sales'], dtype='object')
pd.factorize().pd.factorize() on individual 1D Series columns (e.g., pd.factorize(df['col'])).uniques[codes] when codes contains -1 (sentinel values assigned to NaNs) wraps around to return the last element of the unique index.-1 sentinel codes explicitly or replace missing values prior to factorizing.
Q: How does factorize() differ from get_dummies()?
factorize() returns a single 1D array of integer codes (0, 1, 2...). get_dummies() generates a wide binary matrix of separate 0/1 indicator columns for each category.
Q: Can I decode integer codes back to original string values?
Yes, simply pass the integer code array as an index selector into the returned unique categories: uniques[codes].
Explore interactive code cells, test missing value sentinel codes, run category sorting, and execute self-validating assertions.
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.