简体   繁体   中英

Converting multiple columns variable into dummy variables

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM