python数据框添加新列_基于多个条件向Python Pandas数据框添加新列

我有一个数据集,包含以下各列:

discount tax total subtotal productid

3.98 1.06 21.06 20 3232

3.98 1.06 21.06 20 3232

3.98 6 106 100 3498

3.98 6 106 100 3743

3.98 6 106 100 3350

3.98 6 106 100 3370

46.49 3.36 66.84 63 695

现在,我需要添加一个新的column类,并根据以下条件将其赋值为0或1:if:

discount > 20%

no tax

total > 100

then the Class will 1

otherwise it should be 0

我用一个单一的条件完成了它,但我不知道如何在多个条件下完成它。

这是我试过的wIat:df_full['Class'] = df_full['amount'].map(lambda x: 1 if x > 100 else 0)I have taken a look at all other similar questions but couldn't find any solution for my problem.I have tried all of the above-mentioned posts but stuck on this error:

TypeError: '>' not supported between instances of 'str' and 'int'

在第一次发布答案的情况下,我试着这样做:df_full['class'] = np.where( ( (df_full['discount'] > 20) & (df_full['tax'] == 0 ) & (df_full['total'] > 100) & df_full['productdiscount'] ) , 1, 0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值