-2

I'm trying to turn a float64 value into a binary variable for a predictive model. With it, I was going to do if the price is >= 152.00 then to assign it a 1 else a 0. But I'm getting this error: Getting error The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

I'm confused because everything that I read says to fix to issue, I should use the bitwise operators instead of AND and OR. But I'm not using those. Any ideas what I could do to fix it?

if df_full_train['price'] >= 152.00:
  df_full_train['above_average'] =1
else:
  df_full_train['above_average'] =0
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user14316330
  • 61
  • 1
  • 6
  • You are using the Series in an `if`, which is just as 'bad' as an OR/AND. They all expect a simple True/False. They can't handle the multivalue True/False of the Series test. You apparently want to do some sort iterative test, making the choice for each row, one at a time. – hpaulj Dec 13 '21 at 00:22

1 Answers1

0

So I ended up doing this to get what I wanted. Since 152 is the mean, this worked better and will also continue to work should the mean change in the future.

mean=df_full_train['price'].mean()
df_full_train['above_average']=np.where(df_full_train['price']>=mean,1,0)
user14316330
  • 61
  • 1
  • 6