Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何有条件地将列填充到列表中其他列中的值?_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何有条件地将列填充到列表中其他列中的值?

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(

在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'].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')