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

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']
recmd_prdt_list


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...

type(recmd_prdt_list)


pandas.core.series.Series

# DataFrame of product status

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

type(product_status)


pandas.core.frame.DataFrame

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


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

type(first_row)


str

### Converting the str to list

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


['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']
product_details

ItemCode   Status   DeletedStatus

PLV08       2            False

type(product_details)


pandas.core.frame.DataFrame

product_details['DeletedStatus']


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:
print(itemcode)
product_details = product_status.loc[product_status['ItemCode'] == itemcode]
print(product_details)
if product_details['Status'] == 2 & product_details['DeletedStatus'] == 'False':
activ_product.append(itemcode)


Error :

PLV08
ClientId ItemCode  Status  DeletedStatus
499      2213    PLV08       2          False
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
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__))
954
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?

• 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". – 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:
activ_product.append(itemcode)


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.

• 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. – SRJ577 Sep 4 '19 at 8:21
• have you checked the values you have in first_row_list? – Elliot Sep 4 '19 at 8:51
• yes, I checked. the first_row_list have 3 values which satisfy the condition. – SRJ577 Sep 4 '19 at 9:16