'How can i fix "(df[df['id']==id_value].name == 1).any()" 'The truth value of a Series is ambiguous error'?
The following code will run fine in most cases in python(jupyter), but I'm getting an error from time to time in my web app using the following code:
if (df[df['id']==id_value].name == 1).any():
I haven't been able to determine in which case the error occurred because it was used in web page. In what cases can the famous error below occur?
the truth value of a series is ambiguous. use a.empty a.bool() a.item() a.any() or a.all()
I've tried many cases (eg id=[1,1], name=[1,1]) but jupyter doesn't give me an error.
I have no idea what the heck is the reason.
Thank you in advance.
Solution 1:[1]
Change to isin + loc
if (df.loc[df['id'].isin(id_value),'name']== 1).any():
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BENY |
