1

I have a large dataframe "df":

    ph_level
0   low
1   medium
2   low
3   high
4   low
5   medium

I would like to add 3 binary columns for the low,medium,high data. Below is the output I am seeking:

    ph_level low_binary  medium_binary  high_binary
0   low      1           0              0 
1   medium   0           1              0
2   low      1           0              0
3   high     0           0              1
4   low      1           0              0
5   medium   0           1              0

Is there a very fast way to do this with pandas?

Thanks

1 Answer 1

4

You can use pandas.get_dummies and concatenate the result with the original data frame:

pd.concat([df, pd.get_dummies(df.ph_level).rename(columns = "{}_binary".format)], axis = 1)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Super fast! Thank you Psidom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.