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?
You want to use np.clip
:
>>> import numpy as np
>>> list_values = [-0.01, 0, 0.5, 0.9, 1.0, 1.01]
>>> arr = np.array(list_values)
>>> np.clip(arr, 0.0, 1.0)
array([0. , 0. , 0.5, 0.9, 1. , 1. ])
This is likely the fastest approach, if you can ignore the cost of converting to an array. Should be a lot better for larger lists/arrays.
Involving pandas
in this operation isn't the way to go unless you eventually want a pandas data structure.
This is a rather easy task with numpy
.
import numpy as np
n = np.array([-0.01, 0, 0.5, 0.9, 1.0, 1.01])
n[n > 1] = 1
n[n < 0] = 0
>>> print(n)
[0. 0. 0.5 0.9 1. 1. ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With