Cooper
Cooper

Reputation: 25

delete values from array that corresponds to a specific elements of another array

a = array([1., 0., 1., 0., 1., 0.])
b = np.array([[-1, 1], [-2, 1.8], [-3, -2], [-1.5, 1.5], [4, 6], [-3, 2.3]])

I want to delete points the ones correspond to 0 this means the output should be this:

b = np.array([[-1, 1], [-3, -2], [4, 6]])

I tried this:

for i, j in zip(a, b):
  ind = np.where(i == 0.0) 
  b = np.delete(b, ind, axis=0)
print(b) 

but this deletes randomly. can pls someone help?

Upvotes: 1

Views: 58

Answers (2)

Utkarsh Sharma
Utkarsh Sharma

Reputation: 21

Your code won't work, because once you remove an element from array, indexing changes. Try this:

# create empty numpy array
result = np.empty((0,2), float)

# append if case matches
for i in range(len(a)):
    if(a[i] == 0):
        result = np.append(result, [b[i]], axis = 0)

or simply print:

print(b[a==0])

Upvotes: 2

Alex Alex
Alex Alex

Reputation: 2018

You can try this code:

b=b[a!=0,:]

Upvotes: 1

Related Questions