如何有条件地将列列入列表中的其他列中的值? [英] How to fill a column conditionally to values in an other column being in a list?

查看:93
本文介绍了如何有条件地将列列入列表中的其他列中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大熊猫数据框中,如何有条件地将列的值填充到作为列表的一部分的另一列中的值?



这与这个SO问题,但是当我申请:

  df ['type'] = np.where(df ['food'] in ['apple','banana ','猕猴桃'],'水果','oth。food')

  ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。 

我认为操作符中的被覆盖使用向量..

解决方案

这应该工作

  df ['type'] = np.where(df ['food']。isin(['apple','banana','kiwi']),'fruit','oth 。食物')


In pandas dataframe, how to fill the value of a column conditionally to values in an other column being part of a list ?

This is very similar to this SO question, but when I apply:

df['type'] = np.where(df['food'] in ['apple', 'banana', 'kiwi'], 'fruit', 'oth. food')

I got an error:

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

I suppose the in operator has been not overridden to work with vectors..

解决方案

this should work

df['type'] = np.where(df['food'].isin(['apple', 'banana', 'kiwi']), 'fruit', 'oth. food')

这篇关于如何有条件地将列列入列表中的其他列中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆