I have a list (or some type of array) with nearly all values between 0 and 1, but I have the occasional value that is slightly negative or greater than 1.
list_values = [-0.01, 0, 0.5, 0.9, 1.0, 1.01]
I want to replace negatives with 0 and values greater than 1 with 1.
With only 1 condition, I would use np.where like this:
arr_values = np.where(pd.Series(list_values) < 0, 0, pd.Series(list_values))
To work with multiple conditions, I could define a function and then apply it using a lambda function:
def change_values(value):
if value < 0:
return 0
elif value > 1:
return 1
else:
return value
series_values = pd.Series(list_values).apply(lambda x: change_values(value=x))
Is there a faster way to accomplish this?