Pandas Get Dummies

pandas
Published

December 3, 2023

One-hot encoding is a crucial preprocessing step in machine learning, particularly when dealing with categorical features. Pandas, the powerful Python data manipulation library, provides a straightforward way to achieve this with the get_dummies() function. This post will guide you through its usage with various examples, demonstrating its flexibility and power.

Understanding One-Hot Encoding

Before diving into get_dummies(), let’s understand the concept of one-hot encoding. Imagine you have a categorical feature like “color” with values “red,” “green,” and “blue.” One-hot encoding transforms this single column into three binary columns: “color_red,” “color_green,” and “color_blue.” Each row will have a “1” in the column corresponding to its color and “0” in the others. This numerical representation allows machine learning algorithms to effectively utilize categorical data.

Basic Usage of get_dummies()

Let’s start with a simple example:

import pandas as pd

data = {'color': ['red', 'green', 'blue', 'red', 'green']}
df = pd.DataFrame(data)
print("Original DataFrame:\n", df)

encoded_df = pd.get_dummies(df['color'])
print("\nOne-hot encoded DataFrame:\n", encoded_df)

final_df = pd.concat([df, encoded_df], axis=1)
print("\nFinal DataFrame with one-hot encoded columns:\n", final_df)

This code snippet first creates a DataFrame with a single categorical column “color.” pd.get_dummies(df['color']) then performs the one-hot encoding, creating new columns for each unique color value. Finally, pd.concat() merges the encoded columns back into the original DataFrame.

Handling Multiple Categorical Columns

get_dummies() effortlessly handles multiple categorical columns:

data = {'color': ['red', 'green', 'blue', 'red', 'green'],
        'size': ['small', 'medium', 'large', 'small', 'medium']}
df = pd.DataFrame(data)
print("Original DataFrame:\n", df)

encoded_df = pd.get_dummies(df, columns=['color', 'size'])
print("\nOne-hot encoded DataFrame:\n", encoded_df)

Here, we specify the columns to encode using the columns parameter.

Prefixes and Dummy Variable Traps

To avoid ambiguity and potential issues in your models (like the dummy variable trap), you can customize prefixes for your new columns:

encoded_df = pd.get_dummies(df, columns=['color', 'size'], prefix=['clr', 'sz'])
print("\nOne-hot encoded DataFrame with custom prefixes:\n", encoded_df)

This adds “clr_” and “sz_” prefixes to the generated columns, improving readability and organization. Note that dropping one of the dummy columns per category is best practice to avoid the dummy variable trap – this often happens automatically during model training.

Specifying Data Types

For better control, you can specify the data type of the generated dummy columns:

encoded_df = pd.get_dummies(df, columns=['color', 'size'], dtype=bool)
print("\nOne-hot encoded DataFrame with boolean dtype:\n", encoded_df)

This ensures the generated columns are boolean (True/False), potentially improving memory efficiency.

Using drop_first to Avoid Dummy Variable Trap

As mentioned, the dummy variable trap should be avoided. The drop_first parameter helps with this:

encoded_df = pd.get_dummies(df, columns=['color', 'size'], drop_first=True)
print("\nOne-hot encoded DataFrame with drop_first=True:\n", encoded_df)

Setting drop_first=True removes the first dummy variable for each categorical feature, preventing multicollinearity. This is generally recommended for most modeling tasks.

Handling Sparse Matrices

For datasets with numerous categories, one-hot encoding can lead to high-dimensional sparse matrices. In these situations, consider alternative encoding schemes or explore the sparse=True parameter of get_dummies() for potential memory efficiency gains.

encoded_df = pd.get_dummies(df, columns=['color', 'size'], sparse=True)
print("\nOne-hot encoded DataFrame with sparse=True:\n", encoded_df)

Remember to handle the sparse matrix accordingly in your subsequent analysis.