I have a dataframe "df1":
adj response
beautiful ["She's a beautiful girl/woman, and also a good teacher."]
good ["She's a beautiful girl/woman, and also a good teacher."]
hideous ["This city is hideous, let's move to the countryside."]
And here's the object list:
object=["girl","teacher","city","countryside","woman"]
Code:
df1['response_split']=df1['response'].str.split(",")
After I split it, the dataframe will be like this:
adj response_split
beautiful ["She's a beautiful girl/woman", " and also a good teacher."]
good ["She's a beautiful girl/woman", " and also a good teacher."]
hideous ["This city is hideous", " let's move to the countryside."]
I want to add another column "response_object", if they find the adj in response, they find its object from list object: expected result
adj response_split response_object
beautiful ["She's a beautiful girl/woman", " and also a good teacher."] girl
beautiful ["She's a beautiful girl/woman", " and also a good teacher."] woman
good ["She's a beautiful girl/woman", " and also a good teacher."] teacher
hideous ["This city is hideous", " let's move to the countryside."] city
code:
for i in df1['response_split']:
if df1['adj'] in i:
if any(x in i and x in object):
match = list(filter(lambda x: x in i, object))
df1['response_object']=match
It prints ValueError