How to create a new column if some value match from a list (something like get dummies)

青春壹個敷衍的年華 提交于 2021-02-17 03:22:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!