python - 在python中创建一个新列并使用多个numpy“where”条件分配值
问题描述
我几乎没有什么条件可以工作,比如——
1. if employment is 'salaried' or 'business' and annual income is '<=10 lakhs' and stp flagging is 'NON STP' then result should be 'No Issue'
2. if employment is 'salaried' or 'business' and annual income is '>10 lakhs' and stp flagging is 'STP' then result should be 'No Issue'
3. if employment is 'salaried' or 'business' and annual income is '>10 lakhs' and stp flagging is 'NON STP' then result should be 'Issue'
4. if employment is 'salaried' or 'business' and annual income is '<=10 lakhs' and stp flagging is 'STP' then result should be 'Issue'
我试过这段代码 -
df['Result'] = 'Null'
df['Result']=np.where((df['Employment']=='Salaried')|
(df['Employment']=='Business')&
(df['Annual income of policy owner']=='<= 10 lakh')&
(df['STP flagging'] == 'NON STP'),'No Issue','')
df['Result']=np.where((df['Employment']=='Salaried')|
(df['Employment']=='Business')&
(df['Annual income of policy owner']=='>10 lakh')&
(df['STP flagging'] == 'STP'),'No Issue','')
df['Result']=np.where((df['Employment']=='Salaried')|
(df['Employment']=='Business')&
(df['Annual income of policy owner']=='>10 lakh')&
(df['STP flagging'] == 'NON STP'),'Issue','')
df['Result']=np.where((df['Employment']=='Salaried')|
(df['Employment']=='Business')&
(df['Annual income of policy owner']=='<= 10 lakh')&
(df['STP flagging'] == 'STP'),'Issue','')
但只有最后一行有效,我需要为每个条件分配结果。请帮我修改代码。
解决方案
我认为您没有明确定义您的要求/条件。您可以尝试以下方式numpy.where
:
# Import package
import numpy as np
# Conditions
sb = (df['Employment'] == 'Salaried') | (df['Annual income of policy owner'] == 'Business Owner')
non_stop = (df['STP flagging'] == 'NON STP')
stop = (df['STP flagging'] == 'STP')
lakhs = df['Annual income of policy owner']
# Assignment
df['result'] = np.where(sb & stop & (lakhs.eq('>10 lakh')),'No Issue',
np.where(sb & non_stop & (lakhs.eq('<=10 lakh')),"No issue",
np.where(sb & non_stop & (lakhs.eq('>10 lakh')),"Issue",
np.where(sb & stop & (lakhs.eq('<= 10 lakh')),"Issue","No condition"))))
# Print new column values
>>> df['result'].value_counts()
No condition 5618
Issue 1264
No Issue 618
Name: result, dtype: int64
推荐阅读
- javascript - Select2 和 Datetimepicker 在克隆的 div 中不起作用
- c - 在C中使用空格将char转换为int
- r - 如何使用ggplot在表示数据框两列的两点之间绘制多条线
- verilog - 如何在实例向量中对每个实例进行逻辑操作
- ssis - 与 IBM DB2 Provider for SSIS 连接时默认为 DT_STR (ANSI 1252)
- .net - .NET Core 2.2 - 种子管理员用户
- marklogic - 进行自动化 CloudFormation 部署时是否可以指定 MarkLogic Developer 许可证?
- python - 使用 cx_Oracle 包从 python 环境执行的 oracle SQL 查询中获取动态日期
- javascript - GridView 中的 JavaScript/krajeeDialog.alert 似乎有问题
- reactjs - 提升状态替代从其他组件调用方法