Reputation: 5299
I have dataframe below
col
A a
A b
A c
B d
B e
C f
I would like to get dummy variable
a b c d e f
A 1 1 1 0 0 0
B 0 0 0 1 1 0
C 0 0 0 0 0 1
How can I get this?
I tried
df.col.get_dummies()
But I couldnt groupby.
Upvotes: 1
Views: 288
Reputation: 863166
You need groupby
by index and aggregate max
:
print (df.col.str.get_dummies().groupby(level=0).max())
a b c d e f
A 1 1 1 0 0 0
B 0 0 0 1 1 0
C 0 0 0 0 0 1
Or:
print (pd.get_dummies(df.col).groupby(level=0).max())
a b c d e f
A 1 1 1 0 0 0
B 0 0 0 1 1 0
C 0 0 0 0 0 1
Upvotes: 1