如何检查列表中的所有元素是否存在于pandas列中 [英] How to check if all the elements in list are present in pandas column

查看:138
本文介绍了如何检查列表中的所有元素是否存在于pandas列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dataframe and a list:

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']

我想仅在char列中同时存在ac的情况下获取行.(这里的顺序无关紧要)

I want to get rows only if both a and c both are present in char column.(order doesn't matter here)

预期输出:

       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)]

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

Which is giving me correct output but it is too slow for large dataset so I am looking for more efficient solution.

推荐答案

您可以遍历df.char中的行,并保留namesubset的行:

You could iterate over the rows in df.char and keep those where name is a subset:

names = set(['a','c'])
m = [name.issubset(i) for i in df.char.values.tolist()]

print(df[m])

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

这篇关于如何检查列表中的所有元素是否存在于pandas列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆