How to resolve the error - the truth value of a dataframe is ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all()?

347    Asked by ranjan_6399 in Data Science , Asked on Feb 16, 2023

Firstly I have a pandas series of recommended products (recmd_prdt_list). In this series there is a possibility of the 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 item code 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 :

item code = '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)
in ()
      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/ 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 solve this error?
Answered by Dipika Agarwal

To resolve the error - the truth value of a dataframe is ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all(): First of all, to make logical tests in Python, you should not use & for a single value's equalities (see this) and you should not use question marks around boolean values False and True. Now, concerning your 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.

Your Answer


Parent Categories