🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

pd.get_dummies()

AI & DATA SCIENCE // pd-get-dummies

pd.get_dummies() converts a categorical column into multiple binary (0/1) 'dummy' columns, one per unique category — the standard one-hot encoding technique.

Syntax

pd.get_dummies(data, columns=None, drop_first=False)

Deep Dive Course

get_dummies() creates one new column per unique value in the target categorical column, with each row getting a positive marker in the column matching its original category and a negative marker everywhere else — this one-hot encoding is necessary because most numeric/statistical algorithms can't work directly with text categories, but can work with these binary indicator columns instead. Passing drop_first=True drops the first category's dummy column, since it can always be inferred from the other columns all being negative, which avoids a redundancy issue, multicollinearity, that matters for certain statistical models like linear regression.

1Understanding pd.get_dummies()

get_dummies() creates one new column per unique value in the target categorical column, with each row getting a positive marker in the column matching its original category and a negative marker everywhere else — this one-hot encoding is necessary because most numeric/statistical algorithms can't work directly with text categories, but can work with these binary indicator columns instead. Passing drop_first=True drops the first category's dummy column, since it can always be inferred from the other columns all being negative, which avoids a redundancy issue, multicollinearity, that matters for certain statistical models like linear regression.

💡

Pass drop_first=True when preparing data specifically for a linear regression or similar model sensitive to multicollinearity — keeping every dummy column, including the redundant one that's always inferable from the others, can cause numerical problems for those specific model types.

editor.html
import pandas as pd

df = pd.DataFrame({"color": ["red", "blue", "green", "blue"]})
print(pd.get_dummies(df))
localhost:3000

2Practical Example

Here is a real-world application of pd.get_dummies() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"color": ["red", "blue", "green"]})
print(pd.get_dummies(df, drop_first=True))
localhost:3000

3Best Practices

Follow these guidelines when working with pd.get_dummies():

1. Use get_dummies() to convert categorical text columns into a numeric form that machine learning models can actually use

2. Pass drop_first=True specifically for models sensitive to multicollinearity, like linear/logistic regression, to avoid the redundant reference category

3. Apply get_dummies() consistently across both training and any new/test data, ensuring the exact same set of dummy columns exists in both, since a category present in one but not the other creates a column mismatch

⚠️

Tip: Pass drop_first=True when preparing data specifically for a linear regression or similar model sensitive to multicollinearity — keeping every dummy column, including the redundant one that's always inferable from the others, can cause numerical problems for those specific model types.

editor.html
import pandas as pd

df = pd.DataFrame({"color": ["red", "blue", "green", "blue"]})
print(pd.get_dummies(df))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"color": ["red", "blue", "green", "blue"]})
print(pd.get_dummies(df))
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"color": ["red", "blue", "green"]})
print(pd.get_dummies(df, drop_first=True))

Best Practices

  • Use get_dummies() to convert categorical text columns into a numeric form that machine learning models can actually use
  • Pass drop_first=True specifically for models sensitive to multicollinearity, like linear/logistic regression, to avoid the redundant reference category
  • Apply get_dummies() consistently across both training and any new/test data, ensuring the exact same set of dummy columns exists in both, since a category present in one but not the other creates a column mismatch

Interview Question

Why might dropping one category's dummy column with drop_first=True actually be necessary for some models, rather than just a space-saving convenience?

Hint: Think about what happens when a model tries to fit weights to a set of columns that are perfectly predictable from each other.

When every category gets its own dummy column, the columns become perfectly collinear: for any given row, if you know the value of every dummy column except one, you can always infer that last one exactly, since exactly one of them must be positive and the rest negative. Some models, particularly linear regression solved via certain matrix-inversion methods, can become numerically unstable or fail outright when their input columns have this kind of perfect linear dependency, a problem called multicollinearity. Dropping one category's column removes that redundancy, since the dropped category is now represented by every remaining dummy column being negative, without losing any actual information.

Exercises

MediumPractice using pd.get_dummies() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"color": ["red", "blue", "green", "blue"]})
print(pd.get_dummies(df))

Frequently Asked Questions

Why might dropping one category's dummy column with drop_first=True actually be necessary for some models, rather than just a space-saving convenience?

When every category gets its own dummy column, the columns become perfectly collinear: for any given row, if you know the value of every dummy column except one, you can always infer that last one exactly, since exactly one of them must be positive and the rest negative. Some models, particularly linear regression solved via certain matrix-inversion methods, can become numerically unstable or fail outright when their input columns have this kind of perfect linear dependency, a problem called multicollinearity. Dropping one category's column removes that redundancy, since the dropped category is now represented by every remaining dummy column being negative, without losing any actual information.

Related Functions

df-astypepd-cutboolean-indexing