0

I have a df

name    cars
john    honda,kia
tom honda,kia,nissan
jack    toyota
johnny  honda,kia
tommy   honda,kia,nissan
jacky   toyota

What is a best way using pandas to create a data frame that would add a 1 if car present else 0 to existing df which would look like this.

name    cars    honda   kia nissan  toyota
john    honda,kia   1   1   0   0
tom honda,kia,nissan    1   1   1   0
jack    toyota  0   0   0   1
johnny  honda,kia   1   1   0   0
tommy   honda,kia,nissan    1   1   1   0
jacky   toyota  0   0   0   1

i tried using np.where with multiple conditions as described here but i don't think its the right approach.

1 Answers1

0

That’s exactly what pd.Series.str.get_dummies does, just join it’s result to your dataframe without the cars column:

>>> df.drop(columns=['cars']).join(df['cars'].str.get_dummies(sep=','))
     name  honda  kia  nissan  toyota
0    john      1    1       0       0
1     tom      1    1       1       0
2    jack      0    0       0       1
3  johnny      1    1       0       0
4   tommy      1    1       1       0
5   jacky      0    0       0       1
Cimbali
  • 11,012
  • 1
  • 39
  • 68