Python 如果列表中包含其他子列表的任何字符串,则在列表中创建新列

Python 如果列表中包含其他子列表的任何字符串,则在列表中创建新列,python,pandas,Python,Pandas,假设我有一个df: id words 1 ['apple', 'banana', 'grape'] 2 ['apple', 'banana', 'chocolate'] 3 ['grape', 'popcorn', 'chocolate'] 4 ['grape', 'apple', 'popcorn'] 如何创建一个新列,检查每行列表中是否有“巧克力”或“爆米花”。我尝试过类似的方法,但没有成功: sublist = ['chocolate', 'popcorn'] df['bool

假设我有一个df:

id words
1  ['apple', 'banana', 'grape']
2  ['apple', 'banana', 'chocolate']
3  ['grape', 'popcorn', 'chocolate']
4  ['grape', 'apple', 'popcorn']
如何创建一个新列,检查每行列表中是否有“巧克力”或“爆米花”。我尝试过类似的方法,但没有成功:

sublist = ['chocolate', 'popcorn']

df['boolean_check'] = df['words'].isin(sublist).any()
还尝试:

 df['boolean_check'] = np.where(df['words'].isin(sublist).any(), True, False)
预期结果:

id words                             boolean_check
1  ['apple', 'banana', 'grape']      False
2  ['apple', 'banana', 'chocolate']  True
3  ['grape', 'popcorn', 'chocolate'] True
4  ['grape', 'apple', 'popcorn']     True

尝试使用
explode

np.where(df['words'].explode().isin(sublist).any(level=0), True, False)

完美的非常感谢。不需要np.where()?