我想在loc
函数中有2个条件,但&&
或and
运算符似乎不起作用。:
DF:
business_id ratings review_text
xyz 2 'very bad'
xyz 1 'passable'
xyz 3 'okay'
abc 2 'so so'
mycode的:
我正在尝试收集评分为review_text
并将< 3
列入列表的所有id = xyz
id = 'xyz'
mylist = df.loc[df['ratings'] < 3 and df[business_id] ==id,'review_text'].values.tolist()
我应该得到:
['very bad','passable']
此代码不起作用,我收到错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如何正确使用and
运算符?
答案 0 :(得分:6)
&
逻辑运算符需要and
,因为需要元素and
,请参阅boolean indexing:
id = 'xyz'
mylist=df.loc[(df['ratings'] < 3) & (df['business_id'] == id),'review_text'].values.tolist()
print (mylist)
['very bad', 'passable']
答案 1 :(得分:2)
使用colname(train) = paste("A", colname(train), sep = "")
query
答案 2 :(得分:0)
使用〜进行非逻辑操作
例如:
train.loc[(train['MiscVal'] == 0) & (~train['MiscFeature'].isna())]