Multiple condition using if else statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ck25python
    New Member
    • Jan 2020
    • 20

    Multiple condition using if else statement

    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.

    Code:
    if name in ("spiderman","antman") and salary in range(1000,2000) and city not in ("boston","dubai"):
        new_column=age+salary
    else:
        new_column=age*salary

    Below is the dummy dataset:

    Code:
    name	age	salary	city
    spiderman	20	100	boston
    he-man	21	200	london
    superman	22	300	newyork
    thor	23	400	cairns
    ironman	24	500	sydney
    aquaman	25	600	california
    antman	26	700	spain
    optimus prime	27	100	sydney
    bumble bee	28	170	newyork
    what man	29	290	london
    spiderman	30	1000	auckland
    spiderman	31	1800	beijing
    spiderman	32	3000	delhi
    antman	33	100	tokyo
    antman	34	5000	moscow
    antman	34	1200	dubai
    antman	35	1800	malaysia
    Kind regards,
    CK
    Last edited by gits; Jan 30 '20, 01:55 PM. Reason: added code tags
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    What is your problem? Your code looks fine to me.

    Comment

    • ck25python
      New Member
      • Jan 2020
      • 20

      #3
      Hi,

      Code:
      def fun_check(event):
          
          if name in ("spiderman","antman") and salary in range(1000,2000) and city not in ("boston","dubai"):
              return age+salary
          else:
              return age*salary
      
      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:
      Code:
      NameError: name 'event' is not defined
      Kind regards,
      CK

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        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.

        Comment

        • ck25python
          New Member
          • Jan 2020
          • 20

          #5
          Hi,

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

          Code:
          heros['var1'] = heros['age'] * heros['salary']
          
          heros.loc[((heros['name'] == 'spiderman') | (heros['name'] == 'antman')) & 
                    ((heros['salary'] > 1000) & (heros['salary'] < 2000)) &
                    
                    ((heros['city'] != 'boston') |
                    (heros['city'] != 'dubai')),'var1'] = heros['age'] + heros['salary']
          
          print(heros)
          Is there any efficient way to achieve the result.

          Kind regards,
          CK

          Comment

          Working...