How to Create Dummy Variables in Python with Pandas: A Beginners Guide

preview_player
Показать описание
In this short Python tutorial, you will get the answer to the question "how do you create a dummy variable in Python?". Here, you will use Pandas read_csv, head, unique, and get_dummies method. Note, the last one is what you will use to create dummy variables.

1) Add prefix
2) Remove prefix

After that, you have used a categorical variable with to levels, you will learn how to create dummy variables of a categorical variable with 3 levels. After that, you will learn how to make dummy variables from more than one column and how to change the prefix and prefix separator of your new dummy variables.

If you need to learn more about importing data from CSV files with Pandas:

Now, if you found this valuable, please do comment, like, subscribe, and share it on social media. It's much appreciated!

0:00 Introduction
1:35 Importing data from CSV
2:30 Get the levels of categorical variables
3:08 Example 1: Making a dummy variable (2 factors)
4:18 Example 2: Add the dummy variables to a dataframe
5:15 Example 2: Creating a new dataframe (with dummy vars)
5:33 Removing the prefix from the columns
6:01 Example 3: Categorical variable with 3 factors
6:48 Example 4: Dummy coding 2 columns (variables)
7:04 Example 5: From 3 columns (variables)
7:36 Changing the prefix and prefix separator
8:07 Outro & conclusion: why dummy coding?
Рекомендации по теме
Комментарии
Автор

Thanks, just what I needed to know and easy to understand!

blakegriffey
Автор

hello sir good explanation.
sir,
i having a doubt in data frame hundreds of columns will be their so how we to do domifications for that.
how to loop the domifications incase having hundreds of columns.
for so single column operation is maybe too difficult.
sir please give the answer for this as soon as possible.

rayalacheruvuhemanth
Автор

How would I do the opposite? I have data with a dummy for male (=1 if male) and a separate for female (=1) I want to create a variable called Sex which is a string that says ‘Male’ if the dummy male = 1 and likewise for female? Any help would be greatly appreciated! :)

anonymousduckling