How does python pandas compare the inclusion relationship between two columns of data and write the results in Table 1?

Table 1 add column matching Table 2 key _ word, if Table 1 add contains Table 2 key_word, write group, in Table 1 column 4, if not, mark "not included". PS: I also measured with iteration, Cartesian product also tried, but did not achieve the desired results, please gods, help solve it, thank you! Please take a look at my code below. It"s a piece of cake, which makes you laugh.


it is best not to use for loops.

df1['group'] = df1['add'].apply(lambda x: df2.loc[[y in x for y in df2['key_word']],'group']).stack().reset_index(name='group')['group']

Update:
another way to deal with cases that are not included

def match_group(x):
    for y in df2['key_word']:
        if y in x:
            return y
    return ''

df1['group'] = df1['add'].apply(match_group)
Menu