1

Problem

I am trying to compare values in two columns using an if statement (np.where) however I keep getting an error. I can't figure out for the life of me why, I've used np.where in dataframes previously with no issue.

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

Example

df = pd.DataFrame(
    {'colors': ['red', 'white', 'blue'], 
     'n1': [1, 2, 3], 
     'n2': [4, 6, 7], 
     'n3': [5, 3, 2]
     }
)

df['test'] = np.where(
    df.n1 > df.n2, 
    max(0, df.n1 - df.n3), 
    df.n3
)

Error Message

Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/project/example.py", line 15, in <module>
    df['test'] = np.where(df.n1 > df.n2, max(0, df.n1 - df.n3), df.n3)
  File "C:\Users\user\Continuum\anaconda3\envs\CondaEnv\lib\site-packages\pandas\core\generic.py", line 1442, in __nonzero__
    f"The truth value of a {type(self).__name__} is ambiguous. "
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Process finished with exit code 1

Any help be greatly appreciated.

1 Answer 1

1

Use numpy.maximum:

df['test'] = np.where(
df.n1 > df.n2, 
np.maximum(0, df.n1 - df.n3), 
df.n3)
print (df)
  colors  n1  n2  n3  test
0    red   1   4   5     5
1  white   2   6   3     3
2   blue   3   7   2     2
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! That fixed the issue. Thank you so much for your quick reply. You were too quick, I can't accept answer yet, but will do in 10 mins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.