我正在尝试编写一个函数,它将在给定的数据集中对商品进行分类(我知道,这种方法非常简单)。
看上去:
def classifier(x):
if ('smth' or 'smth' or 'smth') in x:
return 'class1'
elif ('smth' or 'smth' or 'smth') in x:
return 'class2'因此,问题是某些条件不起作用。当我试着单独检查条件时--一切正常。但在功能上出了点问题。
我在熊猫apply-method中使用了thing函数:
data['classes'] = data['subj'].apply(lambda x: classifier(x))发布于 2019-08-19 12:41:33
('smth' or 'smth' or 'smth')执行从左到右的连续逻辑比较,但不检查每个逻辑比较是否出现在目标序列中。
若要检查目标序列x中是否出现预定义列表中的任何值(可迭代),请使用内置any函数:
def classifier(x):
if any(i in x for i in ('a', 'b', 'c')):
return 'class1'
elif any(i in x for i in ('d', 'e', 'f')):
return 'class2'发布于 2019-08-19 12:35:01
你可以用这个:
def classifier(x):
if 'smth' in x or 'smth' in x or 'smth' in x:
return 'class1'
elif 'smth' in x or 'smth' in x or 'smth' in x:
return 'class2'你必须分别检查每一种情况。
https://stackoverflow.com/questions/57556798
复制相似问题