Search code examples
pythondata-cleaning

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


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.


Solution

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