I am working with a list of strings and a dataframe containing strings. Imagine the scenario:
A = ['the', 'a', 'with', 'from', 'on']and a dataframe:
df = {'col1':['string', 'string'], 'col2':['the man from a town', 'the man on a bus']}
I am trying to now create a new column in my data_frame that would present values in column 2 of my data_frame which are in my list A (in this case: the, from, a)
What I wrote was this:
def words_in_A(row):
res=[]
for item in A:
if item in row:
res.extend(item)
return res
df[col3] = df[col2].apply(lambda x: words_in_A(x))
I would expect the output to be a list with multiple values:
col 1 col2 col3 string the man from a town 'the', 'from', 'a' string the man on a bus 'the', 'on', 'a'
But the function just returns the last item ('a') rather than a list. I am not sure why this use of extend() isn't producing a list for me. Please help!