Home > Enterprise >  Python: Convert multiple categorical features to dummy variables efficiently in a loop?
Python: Convert multiple categorical features to dummy variables efficiently in a loop?

Time:11-20

I have a python dataframe and want to convert categorical features to dummy variables. I'm doing a logreg. Right now I only know how to do it manually one by one like below:

sex = pd.get_dummies(train['Sex'], drop_first=True)
embark = pd.get_dummies(train['Embarked'], drop_first=True)
identity = pd.get_dummies(train['Identity'], drop_first=True)
religion = pd.get_dummies(train['Religion'], drop_first=True)

In reality, I actually have to do over 10 of these. How can I get dummies / set the "sex", "embark", "identity", "religion" variables in a more efficient way. Perhaps using a loop?

CodePudding user response:

categories = ['Sex', 'Embarked', 'Identity', 'Religion', ...]
sex, embark, identity, religion, ... = [pd.get_dummies(train[c], drop_first=True) for c in categories]
  • Related