Python Pandas - Computing Dummy Variables



Dummy variables, also known as indicator variables, are binary (0 or 1) variables created to represent categorical data numerically. In data analysis, particularly when working with categorical data, it is often necessary to convert categorical variables into a numerical format. Converting categorical variables into dummy variables is essential for statistical modeling and machine learning, where numerical inputs are required.

Pandas provides two key functions for handling dummy variables −

  • get_dummies(): Converts categorical data into dummy/indicator variables.

  • from_dummies(): Reconstructs the original categorical variable from dummy variables.

In this tutorial, we will explore how to create dummy variables using get_dummies(), customize them with prefixes, handle collinearity, and revert them back to categorical format using from_dummies().

Creating Dummy Variables with get_dummies()

The get_dummies() function in Pandas is used to convert categorical variables of a Series or a DataFrame into dummy variables.

Example: Basic example of creating the Dummy Variables

Here is a basic example of creates dummy variables using the pandas.get_dummies() function.

Open Compiler
import pandas as pd import numpy as np # Create a DataFrame df = pd.DataFrame({"keys": list("aeeioou"), "values": range(7)}) # Display the Input DataFrame print('Input DataFrame:\n',df) # Create dummy variables for the keys column dummies = pd.get_dummies(df["keys"]) print('Resultant Dummy Variables:\n',dummies)

Following is the output of the above code −

Input DataFrame:
keys values
a 0
e 1
e 2
i 3
o 4
o 5
u 6
Resultant Dummy Variables:
a e i o u
0 True False False False False
1 False True False False False
2 False True False False False
3 False False True False False
4 False False False True False
5 False False False True False
6 False False False False True

Creating Dummy Variables with Prefix

The get_dummies() function allows you to add a prefix to the dummy variable column names when converting the categorical variables of a Pandas objects into dummy variables by using the prefix parameter.

Example

This example demonstrates creating dummy variables with a prefix using the pandas.get_dummies() function.

Open Compiler
import pandas as pd import numpy as np # Create a DataFrame df = pd.DataFrame({"keys": list("aeeioou"), "values": range(7)}) # Display the Input DataFrame print('Input DataFrame:\n',df) # Create dummy variables for the keys column dummies = pd.get_dummies(df["keys"], prefix="Col_") print('Resultant Dummy Variables with Prefix:\n',dummies)

Following is the output of the above code −

Input DataFrame:
keys values
a 0
e 1
e 2
i 3
o 4
o 5
u 6
Resultant Dummy Variables with Prefix:
Col__a Col__e Col__i Col__o Col__u
0 True False False False False
1 False True False False False
2 False True False False False
3 False False True False False
4 False False False True False
5 False False False True False
6 False False False False True

Handling Collinearity While Creating Dummy Variables

To avoid collinearity issues in statistical models, you can drop the first dummy variable by setting the drop_first parameter to True.

Example

This example drops the first dummy variable using the drop_first parameter of the pandas.get_dummies() function.

Open Compiler
import pandas as pd import numpy as np # Create a DataFrame df = pd.DataFrame({"keys": list("aeeioou"), "values": range(7)}) # Display the Input DataFrame print('Input DataFrame:\n',df) # Create dummy variables for the keys column dummies = pd.get_dummies(df["keys"], drop_first=True) print('Resultant Dummy Variables with Prefix:\n',dummies)

Following is the output of the above code −

Input DataFrame:
keys values
a 0
e 1
e 2
i 3
o 4
o 5
u 6
Resultant Dummy Variables with Prefix:
e i o u
0 False False False False
1 True False False False
2 True False False False
3 False True False False
4 False False True False
5 False False True False
6 False False False True

Creating Categorical Variables from Dummies

The pandas.from_dummies() function is used to convert the output of get_dummies() back into a categorical Series.

Example

This example demonstrates creating a categorical Series from dummy variables using the pandas.from_dummies() function.

Open Compiler
import pandas as pd import numpy as np # Create a DataFrame with dummy variables df = pd.DataFrame({"Col_a": [0, 1, 0], "Col_b": [1, 0, 1]}) # Display the Input DataFrame print('Input DataFrame:\n',df) # Convert the dummy variables back to categorical original_series = pd.from_dummies(df, sep="_") print('Resultant Categorical Variables:\n',original_series )

Following is the output of the above code −

Input DataFrame:
Col_a Col_b
0 1
1 0
0 1
Resultant Categorical Variables:
Col
b
a
b
Advertisements