0

When I run this code I get the following error:

if products.code == 'attendance':
    print('Attendance Product Detected')
else:
    print('Attendance Not Detected')

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

However, when I type this in the console, it works fine:

products.code == 'attendance'
Out: 
0    True
Name: code, dtype: bool

How can I fix this, so the if and else functions work?

AMC
  • 2,642
  • 7
  • 13
  • 35
  • what are products and codes?are they class and the attributes? – Shubham Shaswat Mar 11 '20 at 11:10
  • Thanks @ShubhamShaswat for replying so quickly. Ayushgupta answered the question –  Mar 11 '20 at 11:12
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – AMC Mar 18 '20 at 22:50
  • Please provide the entire error message, as well as a [mcve]. – AMC Mar 18 '20 at 22:51

2 Answers2

0

try using .item() and moreover try:

if "attendence"==products.code.item():
    print('Attendance Product Detected')
else:
    print('Attendance Not Detected')
ayushgupta
  • 112
  • 11
0

I find another way, but I don't know if it will work. I see your products.code == 'attendance' result like a DataFrame.

if (products.code != 'attendance').empty:
    print('Attendance Product Detected')
else:
    print('Attendance Not Detected')
everfight
  • 420
  • 3
  • 10
  • Please show your data under your question, maybe somebody could help you. It is a bit hard to imagine your data. – everfight Mar 11 '20 at 11:25