0

I have some dirty data that I would like to cleanse by converting the incorrect values to the mean. I currently have the following code;

def convert_bad_data(x):
    if x < 16:
        x == np.mean
        return x
    elif x > 80:
        x == np.mean
        return x
    else:
        return x

When I run this I get the following error

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

The thing is I am not looking to use a boolean so am not sure why I am getting an error about a truth value.

Taylrl
  • 3,601
  • 6
  • 33
  • 44
  • @ayhan Hi, This question has a similar title but they questions are not exactly related. I don't use `and` `or` in my code. – Taylrl Jan 16 '18 at 17:15
  • 1
    From the duplicate: "What you hit was a place where the operator implicitly converted the operands to bool (you used `or` but it also happens for `and`, `if` and `while`)" They are basically the same. You cannot do `some_series and some_other_series` and you also cannot do `if some_series`. – ayhan Jan 16 '18 at 17:19
  • ˋdef issue_ago(date): today = datetime.today() if type(date) == str: date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S.%f') #print date epoch = datetime(1970, 1, 1) diff = today - date date = diff.days /365.25 return date else: return dateˋ – Taylrl Jan 17 '18 at 14:02

1 Answers1

0

The x you're passing is a series, so asking if x < 16 is ambiguous. Instead, you should be using any() if want the condition to trigger if any elements of x are < 16, or all() if you want them all to be.

Tim
  • 2,756
  • 1
  • 15
  • 31
  • I thought I would be iterating through the values in my dataset with each value from each row being set to x each time. – Taylrl Jan 16 '18 at 16:26
  • @Taylrl If you want to do that, you'll need to add an explicit loop over the values before your if/else statements. – Tim Jan 16 '18 at 16:52
  • I have written functions before that iterate over a dataset but yet dont need a loop. I am missing something here. – Taylrl Jan 16 '18 at 16:55