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

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

我有一些脏数据,我想通过将不正确的值转换为平均值来清理这些数据。我目前有以下代码;

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

当我运行这个时,我得到以下错误

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

问题是我不想使用布尔值,所以不确定为什么我会收到关于真值的错误。

你传递的x是一个系列,所以问x < 16是不是有歧义。相反,你应该使用 any() 如果 any x 的元素 < 16 时触发条件,或者 all() 如果你想要它们 全部成为。