我正在使用 pandas 处理 DataFrame,我需要根据某些条件添加一个新列。
我的数据框是:
discount tax total subtotal productid
3 0 20 13 002
10 3 106 94 003
46.49 6 21 20 004
在向 DataFrame添加一个名为Class的新列时,我需要应用一些条件。
条件如下: IF discount > 20 & total > 100 & tax == 0
then Class应为1
否则应为0
这是我尝试过的方法:
def conditions(s):
if (s['discount'] > 20) and (s['tax'] == 0) and (s['total'] > 100):
return 1
else:
return 0
df_full['Class'] = df_full.apply(conditions, axis=1)
但它返回一个错误:
TypeError: ("'>' not supported between 'str' and 'int'", 'occured at index 18')
我该如何解决这个问题?
请帮帮我!
提前致谢!