Python Forum
Applying Function With If
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Applying Function With If
#1
I created a function with if statement to know which table to query and return a value to my dataframe, but I am getting truth value of series is ambiguous error.

Function if statement is as follows:
if s == "A":
   table = "mytable"
elseif s == "Z":
   table = "othertable"
I am creating new column and applying like:
df['newcol'] = df.apply(myfunc(df[col1],df[col2]), axis=1)
I have also tried axis = 0.

Any help is appreciated.
Reply
#2
elseif?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
And please show the full function definition (especially the return statement), and the full text of the error you are getting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Here is a trimmed down version of my function (obviously cannot reference real table names here) Function:
 def myfunc(s, f): if s == "A": table = "mytable" elseif s == "Z": table = "othertable" sql = "SELECT COUNT(*) FROM " + table + " WHERE FIELD1 = " + str(f) result = [] result = pd.read_sql(sql, myconn) return result 
Here is how I am trying to use apply based on a search:
 df['newcol'] = df.apply(myfunc(df['s'],df['f']), axis=1) 
Here is the error
Error:
ValueError Traceback (most recent call last) <ipython-input-62-68c95646bc46> in <module>() ----> 1 df['newcol'] = df.apply(myfunc(df['s'],df['f']), axis=1) <ipython-input-60ecc5d8838b19> in myfunc(s, f) ---->4 if s == "A": raise ValueError("The truth value of a {0} is ambiguous. " "Use a.empty, a.bool(), a.item(), a.any() or a.all()." .format(self.__class__.__name__)) __bool__ = __nonzero__ ValueError" The truth value of a series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Reply
#5
First of all, you still have elseif in there. So that's not the code you're running because that would cause a syntax error.

Second, if you are returning the result of a sql call, that's going to be a sequence of rows matching the call. That's what the error is complaining about: "The truth value of a series is ambiguous." You need to convert the series to a boolean before your return it. Several ways of doing that are mentioned in the error text.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
That elseif is what I had as it worked when I tested a single value. However, I've tried making it a if elif else and that doesn't and just if then, but I think it's still part of my error. In addition, where am I supposed to covert the series?

I tried this but it failed
return result.bool()
Reply
#7
It's if/elif/else:

if condition:
    code
elif other_condition:
    more_code
else:
    yet_more_code
Try bool(result)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
Is it because i have code after the if?

I presume all code must be inside the if statement?
Reply
#9
In the code in your first post, you just need to replace elseif with elif. In your other code I can't tell, because it's all on one line. The indentation is important.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib.pyplot functions create new figures instead of applying to the current one karkas 2 2,045 Jul-09-2020, 08:32 AM
Last Post: karkas
  huge and weird values after applying some calculations karlito 2 2,131 Dec-13-2019, 08:32 AM
Last Post: karlito
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 2,620 Sep-04-2019, 10:04 PM
Last Post: Nuovoq

Forum Jump:

User Panel Messages

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