Вам необходимо:
# create a list of tuples from 1st dataframe
kw = list(zip(df1.IDs, df1.Keywords))
def func(ids, name):
if (ids,name.split(" ")[0]) in kw:
return True
return False
df2['Indicator'] = df2.apply(lambda x: func(x['IDs'],x['Names']), axis=1)
Редактировать
Создать список кортежей с комбинацией идентификаторов и ключевых слов
kw = list(zip(df1.IDs, df1.Keywords))
# [(1234, 'APPLE ABCD'), (1234, 'ORANGE'), (1234, 'LEMONS'), (5346, 'ORANGE'), (5346, 'STRAWBERRY'), (5346, 'BLUEBERRY'), (8793, 'TEA COFFEE')]
unique_kw = list(df1['Keywords'].unique())
# ['APPLE ABCD', 'ORANGE', 'LEMONS', 'STRAWBERRY', 'BLUEBERRY', 'TEA COFFEE']
def samp(x):
for u in unique_kw:
if u in x:
return u
return None
# This will fetch the keywords from column which will be used for compare
df2['indicator'] = df2['Names'].apply(lambda x: samp(x))
df2['indicator'] = df2.apply(lambda x: True if (x['IDs'], x['indicator']) in kw else False, axis=1)
Вывод:
IDs Names indicator
0 1234 APPLE ABCD ONE True
1 5346 APPLE ABCD False
2 1234 NO STRAWBERRY YES False
3 8793 ORANGE AVAILABLE False
4 8793 TEA AVAILABLE False
5 8793 TEA COFFEE True