Home > OS >  Finding a substring (from list of strings) in a string column and add as a new column in Dataframe
Finding a substring (from list of strings) in a string column and add as a new column in Dataframe

Time:02-03

I have the following dataframe (df) containing strings in the "text" column:

text sth
abdcdtext1wrew ...
qwerqdtext2cvufu ...
iuotext3tvbv ...
iuotvbvewre ...

I also have a Series (df_look_for) contains the strings I will be looking for:

look_for
text1
text2
text3

My goal is to check the "text" column whether it contains one of the strings in the "look_for" column. If it contains I want to add the found string as a new column in df. For example:

text sth found_str
abdcdtext1wrew ... text1
qwerqdtext2cvufu ... text2
iuotext3tvbv ... text3
iuotvbvewre ... NaN

So far I am trying to use str.contains() but hasn't succeed yet.

Any help would appreciate it!

CodePudding user response:

Here is an alternative using enter image description here

  • Related