I have a data frame with multiple categorical variables that I need to convert into dummy variables. Gender and region (4 types) are easy with pd.get_dummies
. However, I have several variables that are yes/no
after that. What can I do so that the dummy yes
and no
columns include the variable name? For example, the 'married' variable would turn into married_yes
and married_no
?
Here's my current code and a screenshot of first five lines:
genderdummy=pd.get_dummies(bank_df['gender'])
regiondummy=pd.get_dummies(bank_df['region'])
marrieddummy=pd.get_dummies(bank_df['married'])
cardummy=pd.get_dummies(bank_df['car'])
savingsdummy=pd.get_dummies(bank_df['savings_acct'])
currentdummy=pd.get_dummies(bank_df['current_acct'])
mortgagedummy=pd.get_dummies(bank_df['mortgage'])
pepdummy=pd.get_dummies(bank_df['pep'])
newdata_df=pd.concat([genderdummy,regiondummy,marrieddummy,cardummy,savingsdummy,currentdummy,mortgagedummy,pepdummy], axis=1)
newdata_df.head()
So based on suggestions, here's what I now have:
## HW Part 6: Converting Categorical Variables and Exporting Data
genderdummy=pd.get_dummies(bank_df['gender'])
regiondummy=pd.get_dummies(bank_df['region'])
dummy_vars = [bank_df('married'), bank_df('car'),bank_df('savings_acct'),bank_df('current_acct'),bank_df('mortgage'),bank_df('pep')]
pd.get_dummies(bank_df[dummy_vars])
newdata_df=pd.concat([genderdummy,regiondummy,dummy_vars], axis=1)
newdata_df.head()