How To Create A New Column If Some Value Match From A List (something Like Get Dummies)
I have a df like: text hello how are you hello people hello stackoverflow and a list like this: words = ['Hello','people', 'stackoverflow'] Expected output: text
Solution 1:
Use Series.str.get_dummies
with DataFrame.reindex
for filter columns by list (vallues has to be lowercase for match) and last DataFrame.join
to original:
words = ["hello","people", "stackoverflow"]
df1 = df.join(df['text'].str.get_dummies(' ').reindex(columns=words))
print (df1)
text hello people stackoverflow
0 hello how are you 1 0 0
1 hello people 1 1 0
2 hello stackoverflow 1 0 1
Post a Comment for "How To Create A New Column If Some Value Match From A List (something Like Get Dummies)"