0

I have a working bit of code equivalent to

def foo(x):
    if (df.A == 0 or df.B > df.C):
        return 0
    else:
        return (*some stuff*)

df['D'] = df.apply(lambda x: foo(x),axis=1)

Of course

_.apply(...,axis=1)_ 

is horribly slow and I want to do it efficiently. I tried the following:

crit = (df.A != 0 and df.B <= df.C) #this is just the negation of the previous boolean
df['D'] = 0 
df.loc[crit,'D'] = (*some stuff*)

Here, I get an error:

_ValueError_, _the truth value of a Series is ambiguous_

If my crit only has one clause, it works fine, however. What am I missing here, and how do I fix it? I must confess I find it hard to grok how pandas does these things.

toti08
  • 2,448
  • 5
  • 24
  • 36
Ketil Tveiten
  • 230
  • 2
  • 10

3 Answers3

1

Try with:

crit = ((df.A != 0) & (df.B <= df.C)) 
Joe
  • 12,057
  • 5
  • 39
  • 55
1

Use '&' instead of 'and'. This has been explained in this answer here

Ritesh
  • 3,569
  • 2
  • 8
  • 10
0

Something like this should work. Set a default value for your D column, then replace that value for rows that matches your query.

some_default_value = -1
df["D"] = some_default_value
df.loc[(df.A == 0) | (df.B > df.C), "D"] = 0
Plasma
  • 1,903
  • 1
  • 22
  • 37