Firstly I have a pandas series of recommended product (recmd_prdt_list). In this series there is a possibility of presence of deleted products. So as to remove deleted products from the recommended products, I did the following :

recmd_prdt_list = user_lookup['Recommended items']

0 PLV08, PLPD04, PBC07, 555, PLF02, 963, PLF07, ...

1 123, 345, R922, Asus009, AIMAC, Th001, SAM S9,...

2 LGRFG, LG, 1025, COFMH, 8048, BY7, PLHL4, 569,...

3 COFMH, 5454, 8048, 1025, LG, len123, Th001, PL...

4 LGRFG, AIM-Pro, 569, Asus009, PLHL3, PL04, PLH...

5 PLV08, PLF09, PLF02, PBC04, PLF07, AIM-Pro, PL...



DataFrame of product status

ItemCode  Status DeletedStatus
AIMAC     2      True
AIM-Pro   2      True
SAM S9    2      True
SH MV     2      True
COFMH     2      True
LGRFG     2      True


first_row = user_lookup['Recommended items'][0]

'PLV08, PLPD04, PBC07, 555, PLF02, 963, PLF07, HG8, jealous21, 4'



Converting the str to list

first_row_list = list(first_row .split(","))

['PLV08', ' PLPD04', ' PBC07', ' 555', ' PLF02', ' 963', ' PLF07', ' HG8', ' jealous21', ' 4']

From the first row i took first itemcode to check the deleted status :

product_details = product_status.loc[product_status['ItemCode'] == 'PLV08']
ItemCode   Status   DeletedStatus  

PLV08       2            False



693 False

Name: DeletedStatus, dtype: bool

So as to check the deleted status of each product in the respective row and save to a new list. I wrote the following code :

itemcode = 'PLV08'
activ_product = []
if itemcode in product_status['ItemCode'].values:
    product_details = product_status.loc[product_status['ItemCode'] == itemcode]
    if product_details['Status'] == 2 & product_details['DeletedStatus'] == 'False':

Error :

     ClientId ItemCode  Status  DeletedStatus
499      2213    PLV08       2          False
ValueError                                Traceback (most recent call last)
<ipython-input-35-9507e1ada5f7> in <module>()
      5     product_details = product_status.loc[product_status['ItemCode'] == itemcode]
      6     print(product_details)
----> 7     if product_details['Status'] == 2 & product_details['DeletedStatus'] == 'False':
      8         activ_product.append(itemcode)

~/.virtualenvs/sysg_python3/lib/python3.5/site-packages/pandas/core/generic.py in __nonzero__(self)
    951         raise ValueError("The truth value of a {0} is ambiguous. "
    952                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 953                          .format(self.__class__.__name__))
    955     __bool__ = __nonzero__

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

How to get solve of this error?

  • $\begingroup$ This error always irritates me because it can appear in some pretty random places, but in this case you probably just need to wrap the two conditionals in brackets:if (product_details['Status'] == 2) & (product_details['DeletedStatus'] == False): - note that there should not generally be quotation marks around the word False, since that changes the meaning from a boolean field that's True/False to a string field that's literally the word "False". $\endgroup$
    – Dan Scally
    Sep 2 '19 at 12:30

First of all, to make logical test in Python, you should not use & for a single values equalities (see this) and you should not use question marks around boolean values False and True.

Now, concerning you specific error : When writing product_details['Status'] and product_details['DeletedStatus'] you get each time a Series, which you cannot test for a logical and between them. If you have unique item codes, you can use:

if product_details.iloc[0]['Status'] == 2 and product_details.iloc[0]['DeletedStatus'] == False:

It will simply select the first row of product_details and subset the desired column so that the result is a single value and you can compare it.

  • $\begingroup$ the error solved but got another problem. Above I specified one itemcode directly. For checking the condition for all itemcode in first_row_list I put a for loop. So the code will be : activ_product = [] for item in first_row_list: if item in product_status['ItemCode'].values: product_details = product_status.loc[product_status['ItemCode'] == item] if product_details.iloc[0]['Status'] == 2 and product_details.iloc[0]['DeletedStatus'] == False: activ_product.append(item) After running this the activ_product contains only first itemcode. $\endgroup$
    – SRJ577
    Sep 4 '19 at 8:21
  • $\begingroup$ have you checked the values you have in first_row_list? $\endgroup$
    – Elliot
    Sep 4 '19 at 8:51
  • $\begingroup$ yes, I checked. the first_row_list have 3 values which satisfy the condition. $\endgroup$
    – SRJ577
    Sep 4 '19 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.