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

Ant*_*llo 5 python dataframe pandas

在pandas数据框中,如何有条件地将列的值填充到列表中的另一列中的值?

这非常类似于这个问题,但是当我申请时:

df['type'] = np.where(df['food'] in ['apple', 'banana', 'kiwi'], 'fruit', 'oth. food')
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Run Code Online (Sandbox Code Playgroud)

我想in运算符没有被覆盖以使用向量..

Moh*_*OUI 5

这应该工作

df['type'] = np.where(df['food'].isin(['apple', 'banana', 'kiwi']), 'fruit', 'oth. food')
Run Code Online (Sandbox Code Playgroud)