Table of Contents
The factorize() function in pandas is a useful tool for encoding strings or categorical variables as numbers. It maps the given categories to a range of integers, ensuring that each category is assigned a unique number. This can be useful when working with datasets that contain categorical data as it simplifies the process of performing data analyses. Additionally, encoding categorical data with factorize() can help reduce the amount of memory that a dataset uses, which can be beneficial when dealing with large datasets.
The pandas function can be used to encode strings as numeric values.
You can use the following methods to apply the factorize() function to columns in a pandas DataFrame:
Method 1: Factorize One Column
df['col1'] = pd.factorize(df['col'])[0]
Method 2: Factorize Specific Columns
df[['col1', 'col3']] = df[['col1', 'col3']].apply(lambda x: pd.factorize(x)[0])
Method 3: Factorize All Columns
df = df.apply(lambda x: pd.factorize(x)[0])
The following example shows how to use each method with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'conf': ['West', 'West', 'East', 'East'], 'team': ['A', 'B', 'C', 'D'], 'position': ['Guard', 'Forward', 'Guard', 'Center'] }) #view DataFrame df conf team position 0 West A Guard 1 West B Forward 2 East C Guard 3 East D Center
Example 1: Factorize One Column
The following code shows how to factorize one column in the DataFrame:
#factorize the conf column only df['conf'] = pd.factorize(df['conf'])[0] #view updated DataFrame df conf team position 0 0 A Guard 1 0 B Forward 2 1 C Guard 3 1 D Center
Notice that only the ‘conf’ column has been factorized.
Every value that used to be ‘West’ is now 0 and every value that used to be ‘East’ is now 1.
Example 2: Factorize Specific Columns
The following code shows how to factorize specific columns in the DataFrame:
#factorize conf and team columns only df[['conf', 'team']] = df[['conf', 'team']].apply(lambda x: pd.factorize(x)[0]) #view updated DataFrame df conf team position 0 0 0 Guard 1 0 1 Forward 2 1 2 Guard 3 1 3 Center
Notice that the ‘conf’ and ‘team’ columns have both been factorized.
Example 3: Factorize All Columns
The following code shows how to factorize all columns in the DataFrame:
#factorize all columns df = df.apply(lambda x: pd.factorize(x)[0]) #view updated DataFrame df conf team position 0 0 0 0 1 0 1 1 2 1 2 0 3 1 3 2
Notice that all of the columns have been factorized.