【发布时间】:2018-09-10 04:56:50
【问题描述】:
我有一个包含如下各列的数据集:
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
现在,我需要添加一个新列 Class,并根据以下条件为其分配 0 或 1 的值:
if:
discount > 20%
no tax
total > 100
then the Class will 1
otherwise it should be 0
我已经在一个条件下完成了,但我不知道如何在多个条件下完成它。
这是我尝试过的方法:
df_full['Class'] = df_full['amount'].map(lambda x: 1 if x > 100 else 0)
我查看了所有其他类似的问题,但找不到任何解决我的问题的方法。我已经尝试了上述所有帖子,但遇到了这个错误:
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)
【问题讨论】:
-
请不要发布您的数据或代码的图片
-
不提供数据图像并不意味着根本不提供样本数据。以文本格式提供示例数据。