python中多个条件求值怎么算_在python中,如何根据其他列中的多个条件设置一个列的值?...

我尝试过使用许多答案中的代码来回答与此类似的问题,但是当我试图设置多个条件来决定一个列的值时,我没有找到任何对我有用的方法—我还想用3种不同的方式来实现这一点。在

我得到的数据如下:col1 col2 col3 col4 col5

1 1 1 4 1

0 1 1 1 1

0 0 1 1 1

我想添加另一列,取决于列1-5的值是否大于等于1

看起来像这样:

^{pr2}$

我试过这样的代码:df = pd.read_csv('file.csv',header=0)

m1 = df.col1 >= 1 & df.col2 >= 1 & df.col3 >= 1 & df.col4 >= 1 & df.col5 >= 1

m2 = df.col2 >= 1 & df.col3 >= 1 & df.col4 >= 1 & df.col5 >= 1

m3 = df.col3 >= 1 & df.col4 >= 1 & df.col5 >= 1

df['category'] = np.select([m1, m2, m3], ['certain', 'possible', 'probable'], default='Other')

但这在第一行给出了一个错误:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

为了理解这个错误,我是否需要在运行此代码之前设置值>=1为True,其他值为False?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值