问题
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 Hello people stackoverflow
hello how are you 1 0 0
hello people 1 1 0
hello stackoverflow 1 0 1
回答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
来源:https://stackoverflow.com/questions/59118488/how-to-create-a-new-column-if-some-value-match-from-a-list-something-like-get-d