Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于多个条件在python数据帧中添加列_Python_Dataframe - Fatal编程技术网

基于多个条件在python数据帧中添加列

基于多个条件在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,

我已经给独特的客户打了电话

我还定义了2个变量<代码>贵宾和
首选

我想添加另一个包含特定字符串的列:

  • 如果
    amount\u expense>中的值=VIP
    ,我想添加VIP
  • 如果
    amount\u expense>中的值=preferred
    ,我想添加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
首选值
,您想做什么?