By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,276 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,276 IT Pros & Developers. It's quick & easy.

Multiple condition using if else statement

P: 7
Hi There,

I am trying to replicate one of the SAS functions in Python, not sure how to do it. It would be great if you guys can assist me with this.

Below is the requirement.

I want to perform 2 different functions here.

Expand|Select|Wrap|Line Numbers
  1. if name in ("spiderman","antman") and salary in range(1000,2000) and city not in ("boston","dubai"):
  2.     new_column=age+salary
  3. else:
  4.     new_column=age*salary

Below is the dummy dataset:

Expand|Select|Wrap|Line Numbers
  1. name    age    salary    city
  2. spiderman    20    100    boston
  3. he-man    21    200    london
  4. superman    22    300    newyork
  5. thor    23    400    cairns
  6. ironman    24    500    sydney
  7. aquaman    25    600    california
  8. antman    26    700    spain
  9. optimus prime    27    100    sydney
  10. bumble bee    28    170    newyork
  11. what man    29    290    london
  12. spiderman    30    1000    auckland
  13. spiderman    31    1800    beijing
  14. spiderman    32    3000    delhi
  15. antman    33    100    tokyo
  16. antman    34    5000    moscow
  17. antman    34    1200    dubai
  18. antman    35    1800    malaysia
Kind regards,
CK
3 Weeks Ago #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 626
What is your problem? Your code looks fine to me.
3 Weeks Ago #2

P: 7
Hi,

Expand|Select|Wrap|Line Numbers
  1. def fun_check(event):
  2.  
  3.     if name in ("spiderman","antman") and salary in range(1000,2000) and city not in ("boston","dubai"):
  4.         return age+salary
  5.     else:
  6.         return age*salary
  7.  
  8. heros=heros.assign(new_col=heros.apply(fun_check(event),axis=1))
The error I am getting it from the last line of code, where I am assigning a variable new_col to heros data frame.

Below is the error message:
Expand|Select|Wrap|Line Numbers
  1. NameError: name 'event' is not defined
  2.  
Kind regards,
CK
3 Weeks Ago #3

Expert 100+
P: 626
The error is self-explanatory. No variable named event has been declared in the scope of that statement. Don't know what you expect from this way too small snippet of code.
3 Weeks Ago #4

P: 7
Hi,

I tried another method and it worked but its not a very efficient one.

Expand|Select|Wrap|Line Numbers
  1. heros['var1'] = heros['age'] * heros['salary']
  2.  
  3. heros.loc[((heros['name'] == 'spiderman') | (heros['name'] == 'antman')) & 
  4.           ((heros['salary'] > 1000) & (heros['salary'] < 2000)) &
  5.  
  6.           ((heros['city'] != 'boston') |
  7.           (heros['city'] != 'dubai')),'var1'] = heros['age'] + heros['salary']
  8.  
  9. print(heros)
Is there any efficient way to achieve the result.

Kind regards,
CK
3 Weeks Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.