-2

Here is some data:

import pandas as pd
df=pd.DataFrame( {'a': [1,2,3,4] })
df

    a
0   1
1   2
2   3
3   4

I try to do the statements below:

if df.loc[df['a'] > 1].any() | df.loc[df['a'] < 3].any():
    print("good")
else:
   print("bad")

to print good when any value in column a is bigger than 1 or less than 3 but it gives this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What is wrong with my code please?

L3viathan
  • 26,748
  • 2
  • 58
  • 81
calgary
  • 3
  • 2

2 Answers2

0

If compare scalars use or and also compare only mask, so Series.any return scalar:

if (df['a'] > 1).any() or (df['a'] < 3).any():
    print("good")
else:
   print("bad")

Notice this comment:

Each and every real number is greater than 1 or less than 3.

So maybe you want and:

if (df['a'] > 1).any() and (df['a'] < 3).any():
    print("good")
else:
   print("bad")

Details:

print(df['a'] > 1)
0    False
1     True
2     True
3     True
Name: a, dtype: bool

print((df['a'] > 1).any())
True

print (df['a'] < 3)
0     True
1     True
2    False
3    False
Name: a, dtype: bool

print ((df['a'] < 3).any())
True

Solution should be simplify by Series.between with inclusive=False parameter, but it means more like 1 AND less like 3:

if df['a'].between(1, 3, inclusive=False).any():
    print("good")
else:
   print("bad")


print (df['a'].between(1, 3, inclusive=False))
0    False
1     True
2    False
3    False
Name: a, dtype: bool

print (df['a'].between(1, 3, inclusive=False).any())
True
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • @calgary - Mea culpa, previous answer was wrong, so edited. Here is encessary compare only masks – jezrael Feb 11 '20 at 12:35
0

You can do it the following:

import pandas as pd
df=pd.DataFrame( {'a': [1,2,3,4] })

def condition_method(a):
    return a > 1 or a < 3

df["condition"] = df.a.apply(condition_method)


if df["condition"].any():
    print("Good")
else:
    print("Bad")

But as already mention in the comment by Armali every real number is greater than 1 or less than 3. (Except you have an empty dataframe, in my solution this would be Good.)

Basically what this solution do is that whatever condition you put into the condition_method if any value fulfill the condition you win.

If you want to use loc you have to save the result from the condition in a table like this:

df.loc[df['a'] > 1, 'cond1'].any()

In the end it would look like this:

if df.loc[df['a'] > 1, 'cond1'].any() or df.loc[df['a'] < 3, 'cond2'].any():
   print("good")
else:
   print("bad")
Boendal
  • 2,496
  • 1
  • 23
  • 36