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.