0

I am trying to use a lambda with a dataframe but I am receiving the

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

I am giving an example:

df = pd.DataFrame.from_dict({
    'Product': ['Keyboard', 'Mouse', 'Monitor', 'CPU'],
    'Price': [0, 0.10, 0.15, 0.05],
    'Quantity_Available': [0, 0, 0.17, 0.22]
    })

df=df.drop(['Product'],axis=1)

I am applying this function

df.apply(lambda x: 0 if x<=0.25 else 0.50 if x>0.25 and x<=50 else 0.75 if x>0.50 and x<=0.75 else 1)

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

When I use any() only one value back is given

df.apply(lambda x: 0 if (x<=0.25).any() else 0.50  if (x>0.25 and x<=50).any() else 0.75 if (x>0.50 and x<=0.75).any() else 1)

The outcome is

Price                 0
Quantity_Available    0
dtype: int64

The desired outcome is to have all the values with the correct number

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Valdeloco
  • 13
  • 3
  • 1
    You expect a DataFrame with new values based on the criteria? `df = pd.DataFrame({'Price': [0, 0, 0, 0],'Quantity_Available': [0, 0, 0, 0]})` ?? – wwii Jan 14 '23 at 20:52

0 Answers0