查找列中的值是否包含在python的列表中
我有一个数据框“ a”,另一个有列表“ b”。我想查找列表“ b”中是否存在“ a”值,如下所示。如果存在,则打印“找到”,否则打印“找不到”
a=['BUHARIREST','SAMCO','MCDONALS']
b=['BUHARIRESTAN','BUHARIHOTEL','SAMCOMUL']
a=pd.DataFrame(a)
a['c']=np.NaN
a['c']=a['c'].astype(str)
a.rename(columns={0:'a'},inplace=True)
for i,row in a.iterrows():
if row['a'] in b:
a.at[i,'c']='Found'
else:
a.at[i,'c']='Not Found'
在上述代码的帮助下,我试图实现这一目标,但是“ in”运算符检查列表中是否存在与数据帧“ a”完全相同的单词。
预期结果:
a c
0 BUHARIREST Found
1 SAMCO Found
2 MCDONALS Not Found
实际结果:
a c
0 BUHARIREST Not Found
1 SAMCO Not Found
2 MCDONALS Not Found
答案 0 :(得分:1)
您必须检查b
内的子字符串,因此in
在这里不起作用,而find()
方法是更好的选择。这是您可以尝试的解决方案,尚未优化,但可以使用。
# Checking if values in a['a'] are present as substring in b
# Will return a list with values -1 if no match, 0 if match
a['c'] = a['a'].map(lambda x : [s.find(x) for s in b])
# If a match was found and hence value was 0, -ve sum will not
# be equal to length of b
a['c'] = a['c'].map(lambda x : 'FOUND' if -np.sum(x)!=len(b) else 'NOT FOUND')