Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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_Pandas - Fatal编程技术网

Python 检查两个列表是否在一列中

Python 检查两个列表是否在一列中,python,pandas,Python,Pandas,我有两个列表,其中包含字符串格式的术语。这些术语分为两类:水果和交通工具。我试图显示一个仅包含来自冲突类别的术语对的数据帧。最好的方法是什么?下面是我的列表和数据框的示例。任何帮助都将不胜感激 dataframe: col 1 ['apple', 'truck' ] ['truck', 'orange'] ['pear', 'motorcycle'] ['pear', 'orange' ] ['apple', 'pe

我有两个列表,其中包含字符串格式的术语。这些术语分为两类:水果和交通工具。我试图显示一个仅包含来自冲突类别的术语对的数据帧。最好的方法是什么?下面是我的列表和数据框的示例。任何帮助都将不胜感激

  dataframe:

         col 1                 
  ['apple', 'truck' ]
  ['truck', 'orange']
  ['pear',  'motorcycle']
  ['pear', 'orange' ]
  ['apple', 'pear'  ]
  ['truck', 'car'   ]


  vehicles = ['car', 'truck', 'motorcycle']
  fruits = ['apple', 'orange', 'pear']


  desired output:

        col 2

  ['apple', 'truck' ]
  ['pear', 'motorcycle']
  ['truck', 'orange']

从列表列创建
DataFrame
,通过测试成员资格,然后通过
~
反转掩码,通过按位和-
&
过滤,检查每行至少一个
True
,同时检查列表和最后一个链条件:

另一个解决方案是将
集合
链接(因为标量)并强制转换为
布尔
-空集合转换为

def func(x):
    s = set(x)
    v = set(vehicles)
    f = set(fruits)
    return bool((s & v) and (s & f))

df = df[df['col 1'].apply(func)]
print (df)
                col 1
0      [apple, truck]
1     [truck, orange]
2  [pear, motorcycle]

可能是
np。isin
可能对您有用

super_set = np.array([vehicles,fruits])

def f(x):
    return all(np.isin(super_set,x).sum(axis=1))

df[df.col1.apply(f)]

#
col1
0   [apple, truck]
1   [truck, orange]
2   [pear, motorcycle]

到目前为止你试过什么?
super_set = np.array([vehicles,fruits])

def f(x):
    return all(np.isin(super_set,x).sum(axis=1))

df[df.col1.apply(f)]

#
col1
0   [apple, truck]
1   [truck, orange]
2   [pear, motorcycle]