【发布时间】:2016-08-01 18:12:41
【问题描述】:
在 pandas 数据框中,如何有条件地将列的值填充为列表中另一列中的值?
这个和this SO question很像,但是当我申请时:
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().
我想in 运算符没有被覆盖以使用向量..
【问题讨论】:
-
你在追
df['type'] = np.where(df['food'].isin(['apple', 'banana', 'kiwi']), 'fruit', 'oth. food')