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]