when calling the "np.delete()", I am not interested to define a new variable for the reduced size array. I want to execute the delete on the original numpy array. Any thought?
>>> arr = np.array([[1,2], [5,6], [9,10]])
>>> arr
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
>>> np.delete(arr, 1, 0)
array([[ 1, 2],
[ 9, 10]])
>>> arr
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
but I want:
>>> arr
array([[ 1, 2],
[ 9, 10]])
NumPy arrays are fixed-size, so there can't be an in-place version of np.delete
. Any such function would have to change the array's size.
The closest you can get is reassigning the arr
variable:
arr = numpy.delete(arr, 1, 0)
The delete
call doesn't modify the original array, it copies it and returns the copy after the deletion is done.
>>> arr1 = np.array([[1,2], [5,6], [9,10]])
>>> arr2 = np.delete(arr, 1, 0)
>>> arr1
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
>>> arr2
array([[ 1, 2],
[ 9, 10]])
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