python - 如何检查列表中的所有元素是否都存在于 pandas 列中

标签 python python-3.x pandas

我有一个数据框和一个列表:

df = pd.DataFrame({'id':[1,2,3,4,5,6,7,8], 
    'char':[['a','b'],['a','b','c'],['a','c'],['b','c'],[],['c','a','d'],['c','d'],['a']]})

names = ['a','c']

只有 ac 都出现在 char 列中时,我才想获取行。(这里的顺序无关紧要)

预期输出:

       char  id                                                                                                                      
1  [a, b, c]   2                                                                                                                      
2     [a, c]   3                                                                                                                      
5  [c, a, d]   6   

我的努力

true_indices = []
for idx, row in df.iterrows():
    if all(name in row['char'] for name in names):
        true_indices.append(idx)


ids = df[df.index.isin(true_indices)]

这给了我正确的输出,但它对于大型数据集来说太慢了,所以我正在寻找更有效的解决方案。

最佳答案

使用pd.DataFrame.apply:

df[df['char'].apply(lambda x: set(names).issubset(x))]

输出:

   id       char
1   2  [a, b, c]
2   3     [a, c]
5   6  [c, a, d]

关于python - 如何检查列表中的所有元素是否都存在于 pandas 列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55745235/

相关文章:

python - 不止是多个set的交集,还有union和update

python - 在 Python 中,我们应该在参数列表中设置 Optional[str] = None 吗?

python - 确定何时没有数据库行与 Python mysql.connector 匹配

python - 多索引数据框中的多行选择

python - df.hist() 与 df.plot.hist()?

python - 如果除一列之外的所有列均为空,如何删除整行?

python - 向下移动一行然后重命名该列

python - 我如何为 Windows 安装 xgoogle?

python - pytest输出结果在pycharm中乱码

python - 值错误 : Found arrays with inconsistent numbers of samples