I have a df as
name category dummy
USA fx,ft,fe 1
INDIA fx 13
I need to convert this as
name category_fx categoty_ft category_fe dummy
USA True True True 1
INDIA True False False 13
tried with series.explode() function but not getting this output.
>Solution :
You can use str.get_dummies and astype(bool) to convert your strings to new columns of booleans, then add_prefix to change the column names, and finally join:
df2 = (df.drop(columns='category)
.join(df['category']
.str.get_dummies(sep=',')
.astype(bool)
.add_prefix('category_')
)
)
or, for modification of the original dataframe:
df = df.join(df.pop('category')
.str.get_dummies(sep=',')
.astype(bool)
.add_prefix('category_'))
output:
name category_fe category_ft category_fx
0 USA True True True
1 INDIA False False True