基于多个条件在python数据帧中添加列
我已经给独特的客户打了电话 我还定义了2个变量<代码>贵宾和基于多个条件在python数据帧中添加列,python,dataframe,Python,Dataframe,我已经给独特的客户打了电话 我还定义了2个变量贵宾和首选 我想添加另一个包含特定字符串的列: 如果amount\u expense>中的值=VIP,我想添加VIP 如果amount\u expense>中的值=preferred,我想添加preferred 这就是我所做的: unique_customers['Customer Status'] = np.where(unique_customers[['amount_spent']] >= VIP,
首选
我想添加另一个包含特定字符串的列:
- 如果
,我想添加VIPamount\u expense>中的值=VIP
- 如果
,我想添加preferredamount\u expense>中的值=preferred
unique_customers['Customer Status'] =
np.where(unique_customers[['amount_spent']] >= VIP,
'VIP', (unique_customers[['amount_spent']] >= preferred,
'preferred', 'other'))
这是行不通的。我收到以下错误:
ValueError:传递的项目数错误3,放置意味着1
我在这里做错了什么?你看,
numpy。其中
有三个参数:(条件、值如果为真,值如果为假)
。您正在将一个包含三个元素的元组
传递给第三个参数,但我认为您的意思是:
unique_customers['Customer Status'] =
np.where(unique_customers[['amount_spent']] >= VIP,
'VIP',
np.where(unique_customers[['amount_spent']] >= preferred,
'preferred',
'other'))
您正在使用带有多个条件的
np.where
。使用np。选择。此外,如果该值大于VIP
和首选值
,您想做什么?