python - Comparing common strings in two pandas dataframe columns -


i have pandas data frame follows:

coname1        coname2 apple          [microsoft, apple, google] yahoo          [american express, jet blue] gap inc       [american eagle, walmart, gap inc] 

i want create new column flags whether string in coname1 contained in conames. so, above example, dataframe be:

coname1        coname2                               isin apple          [microsoft, apple, google]            true yahoo          [american express, jet blue]          false gap inc       [american eagle, walmart, gap inc]     true 

set frame:

df =pd.dataframe({'coname1':['apple','yahoo','gap inc'],           'coname2':[['microsoft', 'apple', 'google'],['american express', 'jet blue'],                      ['american eagle', 'walmart', 'gap inc']]}) 

try this:

df['isin'] =df.apply(lambda row: row['coname1'] in row['coname2'],axis=1) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -