Home > Software engineering >  Delete elements from numpy arrays at the same position by using zip() and np.delete()
Delete elements from numpy arrays at the same position by using zip() and np.delete()

Time:10-25

I got two 1D arrays of same length (it and l) and I want to delete the element y in l for l < ls and also delete element x in it at the same position. I tried:

for x, y in zip(it, l):
        if y < ls:
            np.delete(l,y)
            np.delete(it,x)

But it is not working and there is the Warning:

"DeprecationWarning: using a non-integer array as obj in delete will result in an error in the future" np.delete(l,y)

"DeprecationWarning: using a non-integer array as obj in delete will result in an error in the future np.delete(it,x)"

CodePudding user response:

Here you have an example.

You have to indicate the index of the element that you want to remove :

import numpy as np

it = np.asarray(range(10))
l = np.asarray(range(10))

ls = 4
index = []
for i, y in enumerate(l):
        if y < ls:
            index.append(i)

l = np.delete(l, index)
it = np.delete(it, index)

Results :

(array([4, 5, 6, 7, 8, 9]), array([4, 5, 6, 7, 8, 9]))

Shorter version :

index = np.argwhere(l < ls)
l = np.delete(l, index)
it = np.delete(it, index)
  • Related