Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple conditions
#1
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.
If name in (“spiderman”,”antman”) and salary between (1000,2000) and city not in ("boston","dubai") then
    new_column=age+salary
else:
    new_column=age*salary 
Below is the dummy data set.

Output:
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
Reply
#2
python code reads a lot like speech, so your requirement is not far off from the actual source 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 
For the if condition to pass all 3 of those conditions have to return True.
Recommended Tutorials:
Reply
#3
(Jan-30-2020, 03:42 AM)metulburr Wrote: python code reads a lot like speech, so your requirement is not far off from the actual source 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 
For the if condition to pass all 3 of those conditions have to return True.

Hi There,
I tried to do it as per your instruction, but it failed.
It would be great if you can provide me the complete code. I am pretty much new to python.

Thanks,
CK

Hi There,
I tried to do it as per your instruction, but it failed.
It would be great if you can provide me the complete code. I am pretty much new to python.

Thanks,
CK
Reply
#4
You will have to provide me with the traceback of why it failed.
Recommended Tutorials:
Reply
#5
Hi,
This is the code I have used and the following is the error message.


def fun_check():
    
    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,axis=1))
Error:
TypeError: ('fun_check() takes 0 positional arguments but 1 was given', 'occurred at index 0')
Reply
#6
(Jan-30-2020, 12:48 PM)Chandan Wrote: heros=heros.assign(new_col=heros.apply(fun_check,axis=1))
If fun_check is a callback function an event is going to be sent as an argument to that. This would be a reason why the error states it takes 0 arguments but one was given.

What is heros.apply?
and what is heros.assign?
Are you using Tkinter?

You can use lambdas. Here is more info on lambdas. This is assuming that this is the issue. Would need to see more code than what your giving to determine what is going on.
def fun_check(event):
...
heros=heros.assign(new_col=heros.apply(lambda:fun_check(event),axis=1))
Recommended Tutorials:
Reply
#7
Are you using Pandas?
Then that info should be provided in you first post,as the answer may be quite different.
Example how a check look in Pandas,eg using name and salary.
import pandas as pd

df = pd.read_clipboard()
status_check = df[df['name'].str.contains('spiderman|antman') & (df['salary'].between(1000, 2000))]
print(status_check)
Output:
name age salary city 10 spiderman 30 1000 auckland 11 spiderman 31 1800 beijing 15 antman 34 1200 dubai 16 antman 35 1800 malaysia
Reply
#8
Hi metulburr,
Heros is the data frame.
I went through some of the code which was posted in the forum and I replicated that here, will go through some document to understand better.

Hi snippsat,
Yes, I am using Pandas.
I wanted to create a new column by satisfying the above condition.
I tried with the example you have provided and it works, when I used my logic as above to create a new column it fails. Will look through some document to understand better.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do you format Update statement with multiple conditions hammer 4 2,040 Dec-16-2021, 10:49 PM
Last Post: hammer
  Multiple conditions when indexing an array with or without np.where noob2305 1 2,608 Oct-25-2020, 02:06 PM
Last Post: jefsummers
  Multiple conditions, one is null moralear27 1 2,165 Sep-13-2020, 06:11 AM
Last Post: scidam
  Help with multiple conditions on ATM program jakegold98 6 9,128 Dec-06-2017, 05:18 PM
Last Post: jakegold98
  Simplifying multiple "or" conditions in if statement. rhubarbpieguy 8 101,903 Jul-22-2017, 12:19 PM
Last Post: rhubarbpieguy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020