JerryKur
JerryKur

Reputation: 7519

How can I filter a numpy array based on another numpy array?

I have two numpy arrays that were created by splitting one array. X has 7 columns and Y has 1 column.

I am filtering X with :

X[(X[:,2] != 0) & (X[:,1] != 0) & (X[:,3] != 0) & (X[:,4] != 0)]

This gives me the correct rows of X. How do I get the rows in Y with the matching row indices?

Upvotes: 0

Views: 898

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113978

the same way you get X

mask = (X[:,2] != 0) & (X[:,1] != 0) & (X[:,3] != 0) & (X[:,4] != 0)
# mask is a list of [True,False,True,...]
print X[mask]
print Y[mask]

Upvotes: 1

Related Questions