Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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 usingget_dummies(), customize them with prefixes, handle collinearity, and revert them back to categorical format usingfrom_dummies().

Creating Dummy Variables with get_dummies()

Theget_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 thepandas.get_dummies() function.

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

Following is the output of the above code −

Input DataFrame:
keysvalues
a0
e1
e2
i3
o4
o5
u6
Resultant Dummy Variables:
aeiou
0TrueFalseFalseFalseFalse
1FalseTrueFalseFalseFalse
2FalseTrueFalseFalseFalse
3FalseFalseTrueFalseFalse
4FalseFalseFalseTrueFalse
5FalseFalseFalseTrueFalse
6FalseFalseFalseFalseTrue

Creating Dummy Variables with Prefix

Theget_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 theprefix parameter.

Example

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

import pandas as pdimport numpy as np# Create a DataFramedf = pd.DataFrame({"keys": list("aeeioou"), "values": range(7)})# Display the Input DataFrameprint('Input DataFrame:\n',df)# Create dummy variables for the keys columndummies = 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:
keysvalues
a0
e1
e2
i3
o4
o5
u6
Resultant Dummy Variables with Prefix:
Col__aCol__eCol__iCol__oCol__u
0TrueFalseFalseFalseFalse
1FalseTrueFalseFalseFalse
2FalseTrueFalseFalseFalse
3FalseFalseTrueFalseFalse
4FalseFalseFalseTrueFalse
5FalseFalseFalseTrueFalse
6FalseFalseFalseFalseTrue

Handling Collinearity While Creating Dummy Variables

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

Example

This example drops the first dummy variable using thedrop_first parameter of thepandas.get_dummies() function.

import pandas as pdimport numpy as np# Create a DataFramedf = pd.DataFrame({"keys": list("aeeioou"), "values": range(7)})# Display the Input DataFrameprint('Input DataFrame:\n',df)# Create dummy variables for the keys columndummies = 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:
keysvalues
a0
e1
e2
i3
o4
o5
u6
Resultant Dummy Variables with Prefix:
eiou
0FalseFalseFalseFalse
1TrueFalseFalseFalse
2TrueFalseFalseFalse
3FalseTrueFalseFalse
4FalseFalseTrueFalse
5FalseFalseTrueFalse
6FalseFalseFalseTrue

Creating Categorical Variables from Dummies

Thepandas.from_dummies() function is used to convert the output ofget_dummies() back into a categorical Series.

Example

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

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

Following is the output of the above code −

Input DataFrame:
Col_aCol_b
01
10
01
Resultant Categorical Variables:
Col
b
a
b
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp