1

I have a DF, with 2 columns, id and symptoms (int, string) it looks like this:

id      symptoms
1       symptom1;symptom2 
2       symptom2;symptom3 
3       symptom1;symptom3;symptom4

I want it to change the columns to id, symptom1, symptom2, symptom3, symptom4,... and the values in the new columns will be true/false depending on the original column. so the first row will be: [id1, true, true, false, false,...]

I found this question which is close: Changing a string column into several boolean columns using pandas

but in my case I might have several symptoms and I couldn't get it to work.

any advice on how to get to this?

Alon.b
  • 61
  • 4

1 Answers1

1

Try with get_dummies

out = df.join(df.pop('symptoms').str.get_dummies(';').astype(bool))
BENY
  • 317,841
  • 20
  • 164
  • 234