pandas python的条件求和

2024-03-29 11:06:36 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好

请您帮忙。 因此,我想使用customers Income Proxy创建一个名为df['Income_Status']的新列。在

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

我的数据集如下所示:

^{2}$

我该如何创建一个列,通过使用提供的条件来提供收入状态。 我如何用Pandas python格式编写这个条件呢?在


Tags: 数据pandasdf状态格式statusmedwhere
1条回答
网友
1楼 · 发布于 2024-03-29 11:06:36

我想你需要^{}

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  

相关问题 更多 >