Python 如何有条件地将列填充到列表中其他列中的值?
在dataframe中,如何有条件地将一列的值填充为列表中另一列的值 这非常类似于,但当我申请时:Python 如何有条件地将列填充到列表中其他列中的值?,python,pandas,dataframe,Python,Pandas,Dataframe,在dataframe中,如何有条件地将一列的值填充为列表中另一列的值 这非常类似于,但当我申请时: df['type'] = np.where(df['food'] in ['apple', 'banana', 'kiwi'], 'fruit', 'oth. food') 我有一个错误: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(
df['type'] = np.where(df['food'] in ['apple', 'banana', 'kiwi'], 'fruit', 'oth. food')
我有一个错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我假设中的运算符未被覆盖以处理向量。我认为您可以使用:
这应该行得通
df['type'] = np.where(df['food'].isin(['apple', 'banana', 'kiwi']), 'fruit', 'oth. food')
你要找的是df['type']=np.where(df['food'].isin(['apple','banana','kiwi']),'fruit','oth.food')
:)我们的帖子之间只有20秒。很好。@jezrael:谢谢两位。。我希望我能将这两个答案都添加为可接受的答案;-)是的,如果你能把两者都加上:-)我想我能在你的皮肤上做些什么-也许只能把两种解决方案都加起来。但是你输了+2
,嗯。但是这取决于你,你怎么做。没问题。天气真好。
df['type'] = np.where(df['food'].isin(['apple', 'banana', 'kiwi']), 'fruit', 'oth. food')