gpt4 book ai didi

python - 根据多个条件向 Python Pandas DataFrame 添加新列

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:08 25 4
gpt4 key购买 nike

我有一个包含各种列的数据集,如下所示:

折扣税总小计 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 并根据以下条件为其分配值 01 :

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)

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)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com