multiple conditions - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: multiple conditions (/thread-24078.html) |
multiple conditions - Chandan - Jan-30-2020 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*salaryBelow is the dummy data set. Kind regards,CK RE: multiple conditions - metulburr - Jan-30-2020 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*salaryFor the if condition to pass all 3 of those conditions have to return True. RE: multiple conditions - Chandan - Jan-30-2020 (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 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 RE: multiple conditions - metulburr - Jan-30-2020 You will have to provide me with the traceback of why it failed. RE: multiple conditions - Chandan - Jan-30-2020 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))
RE: multiple conditions - metulburr - Jan-30-2020 (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)) RE: multiple conditions - snippsat - Jan-30-2020 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)
RE: multiple conditions - Chandan - Jan-31-2020 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. |