Python 根据元素是否在列中选择数据帧的行';s列表

Python 根据元素是否在列中选择数据帧的行';s列表,python,pandas,Python,Pandas,我目前有这样一个数据帧: col1 col2 col3 0 0 1 ['a', 'b', 'c'] 1 2 3 ['d', 'e', 'f'] 2 4 5 ['g', 'h', 'i'] 我要做的是选择col3列表中包含特定值的行。例如,我最初运行的代码是: df.loc['a' in df['col3']] 但我得到了以下

我目前有这样一个数据帧:

      col1      col2            col3
 0     0         1         ['a', 'b', 'c']
 1     2         3         ['d', 'e', 'f']
 2     4         5         ['g', 'h', 'i']
我要做的是选择
col3
列表中包含特定值的行。例如,我最初运行的代码是:

df.loc['a' in df['col3']]
但我得到了以下错误:

KeyError: False
我看了一下这个问题:但它并没有完全回答我的问题。我尝试了答案中建议的解决方案,但没有效果


我将如何处理这个问题?谢谢。

使用列表理解测试每个列表:

df1 = df[['a' in x for x in df['col3']]]
print (df1)
   col1  col2       col3
0     0     1  [a, b, c]
或使用:

或者创建
数据帧
并通过以下方式进行测试:

使用:


熊猫专家耶兹雷尔是一个真正的活生生的保护者
df1 = df[df['col3'].map(lambda x: 'a' in x)]
#alternative
#df1 = df[df['col3'].apply(lambda x: 'a' in x)]
df1 = df[pd.DataFrame(df['col3'].tolist()).eq('a').any(axis=1)]
df = df[df.testc.map(lambda x: 'a' in x)]