【问题标题】:Conditional Sum in pandas python熊猫python中的条件和
【发布时间】:2018-01-22 09:43:44
【问题描述】:

大家好

我希望得到您的帮助。 所以我想使用客户收入代理创建一个名为 df['Income_Status'] 的新列。

Where 
  1 = High Avg Income,        
  2 = Med Avg Income,         
  3= Low Avg Income

这是我的数据集的样子:

  Customer_id     Income_Proxy  Average_Income_Proxy   Standard_dev_Income_Proxy

    123              7681559.15       44288.02                176568.76
    456              15156.29         44288.02                176568.76
    789              50497.69         44288.02                176568.76
    096              44138.41         44288.02                176568.76
    158              67866.45         44288.02                176568.76

The condition is:
IF INCOME_PROXY <= MEAN_INCOME_PROXY - (0.5)*&STD_INCOME_PROXY
then High Avg Income

IF INCOME_PROXY >= MEAN_INCOME_PROXY + (0.5)*STD_INCOME_PROXY
then Low Avg Income

else Low Avg Income

如何使用所提供的条件创建一个列来为我提供收入状态。 我将如何用 Pandas python 格式编写这个条件?

【问题讨论】:

  • 问题似乎是错误的,then Low Avg Income else Low Avg Income

标签: python python-3.x python-2.7 pandas


【解决方案1】:

我觉得你需要numpy.select:

m1 = df.Income_Proxy <= df.Average_Income_Proxy - (0.5)* df.Standard_dev_Income_Proxy
m2 = df.Income_Proxy >= df.Average_Income_Proxy + (0.5)* df.Standard_dev_Income_Proxy

df['Income_Status'] = np.select([m1,m2], [1,3], default=2)
print (df)
   Customer_id  Income_Proxy  Average_Income_Proxy  Standard_dev_Income_Proxy  \
0          123    7681559.15              44288.02                  176568.76   
1          456      15156.29              44288.02                  176568.76   
2          789      50497.69              44288.02                  176568.76   
3           96      44138.41              44288.02                  176568.76   
4          158      67866.45              44288.02                  176568.76   

   Income_Status  
0              3  
1              2  
2              2  
3              2  
4              2  

【讨论】:

猜你喜欢
  • 2022-12-11
  • 2018-08-23
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 2021-03-01
  • 2022-01-02
相关资源
最近更新 更多