Reputation: 539
I have a pandas DataFrame, there is a column with values like a,b,c i.e. string splited by ','. Now I want to create new columns, for example, for a,b,c there would be new column a, column b, column c. then the data with a,b,c would get a value of true on the three columns, the data with a,b,e would get true on columns a and b but false on c, maybe it is more clearly to see the picture below. How to do this?
Upvotes: 1
Views: 44
Reputation: 863301
Use str.get_dummies
with cast to bool by astype
and add column B
by join
:
df1 = df['A'].str.get_dummies(',').astype(bool).join(df['B'])
print (df1)
a b c f B
0 True True True False 3
1 False True True True 4
More general solution with pop
for extract column A
:
df = pd.DataFrame({'A':['a,b,c','b,c,f'], 'B':[3,4], 'C':[7,3]})
print (df)
A B C
0 a,b,c 3 7
1 b,c,f 4 3
df1 = df.pop('A').str.get_dummies(',').astype(bool).join(df)
print (df1)
a b c f B C
0 True True True False 3 7
1 False True True True 4 3
Upvotes: 3