Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查列表中的所有元素是否都在列中_Python_Python 3.x_Pandas - Fatal编程技术网

Python 如何检查列表中的所有元素是否都在列中

Python 如何检查列表中的所有元素是否都在列中,python,python-3.x,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'] 我只想在char列中同时存在a和c时获取行。(这里的顺序不重要) 预期输出: char id

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

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
列中同时存在
a
c
时获取行。(这里的顺序不重要)

预期输出:

       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]

将列表理解用于:

另一个解决方案包括:

性能取决于行数和匹配值数:

df = pd.concat([df] * 10000, ignore_index=True)

In [270]: %timeit df[df['char'].apply(lambda x: set(names).issubset(x))]
45.9 ms ± 2.26 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [271]: %%timeit
     ...: names = set(['a','c'])
     ...: [names.issubset(set(row)) for _,row in df.char.iteritems()]
     ...: 
46.7 ms ± 5.51 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)


In [272]: %%timeit
     ...: df[[set(names).issubset(x) for x in df['char']]]
     ...: 
45.6 ms ± 1.26 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [273]: %%timeit
     ...: df[df['char'].map(set(names).issubset)]
     ...: 
18.3 ms ± 2.96 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [274]: %%timeit
     ...: n = set(names)
     ...: df[df['char'].map(n.issubset)]
     ...: 
16.6 ms ± 278 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [279]: %%timeit
     ...: names = set(['a','c'])
     ...: m = [name.issubset(i) for i in df.char.values.tolist()]
     ...: 
19.2 ms ± 317 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

您可以从名称列表中生成一个集合,以便更快地查找,并使用检查集合中的所有元素是否都包含在列列表中:

names = set(['a','c'])
df[df['char'].map(names.issubset)]

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

df['char']=df['char'].apply(lambda x: x if ("a"in x and "c" in x) else np.nan)
print(df.dropna())
输出:

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

这个比其他的快。谢谢:-)@yatu-hmm,对我来说不是,但实际数据似乎不同
%%timeit names=set(['a','c'])m=[name.issubset(i)for i in df.char.values.tolist()]19.2ms±317µs/循环(平均±标准偏差为7次运行,每个循环100次)
df['char']=df['char'].apply(lambda x: x if ("a"in x and "c" in x) else np.nan)
print(df.dropna())
   id       char
1   2  [a, b, c]
2   3     [a, c]
5   6  [c, a, d]