Posts: 10
Threads: 5
Joined: Dec 2019
Jan-30-2020, 12:55 AM
(This post was last modified: Jan-30-2020, 01:56 AM by Larz60+.)
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
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jan-30-2020, 03:42 AM
(This post was last modified: Jan-30-2020, 03:43 AM by metulburr.)
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:
Posts: 10
Threads: 5
Joined: Dec 2019
Jan-30-2020, 10:31 AM
(This post was last modified: Jan-30-2020, 11:03 AM by Chandan.)
(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
Posts: 5,151
Threads: 396
Joined: Sep 2016
You will have to provide me with the traceback of why it failed.
Recommended Tutorials:
Posts: 10
Threads: 5
Joined: Dec 2019
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')
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jan-30-2020, 01:07 PM
(This post was last modified: Jan-30-2020, 01:07 PM by metulburr.)
(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:
Posts: 7,324
Threads: 123
Joined: Sep 2016
Jan-30-2020, 01:32 PM
(This post was last modified: Jan-30-2020, 01:32 PM by snippsat.)
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
Posts: 10
Threads: 5
Joined: Dec 2019
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.
|