I'm thinking to convert multiple columns of categorical variables in a data frame to dummy variables using pandas. The code below works, but it seems too long. May I know how do I combine these 3 lines in a single line of code, please? Thank you
X = pd.get_dummies(df.height, prefix=['height'])
Y = pd.get_dummies(df.weight, prefix=['weight'])
Z = pd.get_dummies(df.age, prefix=['age'])**
You can use:
pd.get_dummies(data=df, columns=["height", "weight", "age"])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.