0

I have few conditions to work upon, like -

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'

I have tried this code -

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','')

but only the last line is working, I need to assign the result for each and every condition. Please help me to modify the code.

2
  • If you mean you only see the results from the last line after running the code, that would be because you keep overwriting the 'Result' column. Commented Oct 6, 2021 at 10:31
  • yes exactly, how to correct this? Commented Oct 6, 2021 at 10:32

1 Answer 1

1

I don't think you defined your requirements / conditions clearly. You could try something along the lines below with 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.