Pratibha Gupta
Pratibha Gupta

Reputation: 309

np.where multiple condition on multiple columns

I have a 2D array and a working code with np.where() condition on one column. I need to enhance this code by adding one more condition by adding an extra filter.

for an array like this:

array([[  1,   2,   3],
       [ 11,  22,  33],
       [101, 202, 303],
       [100, 200, 303],
       [111, 222, 333]])

my condition is working fine where index 2's column value is 303

a = np.delete(a, np.where(a[:, 2] == 303), axis=0)

Now I need to add one more condition, where index 1's value equals to 200. I tried adding np.all for multiple conditions as mentioned below, but it doesn't solve the purpose.

a = np.delete(a, np.where(np.all((a[:, 2] == 303) & (a[:,1] == 200)) ), axis=0)

any help is appreciated.

Upvotes: 0

Views: 1098

Answers (2)

NaN
NaN

Reputation: 2312

Using logical_and explicitly, then recasting to 'not' is another possibility.

w = np.logical_and(a[:, 2] == 303, a[:, 1] == 200)

a[~w]

array([[  1,   2,   3],
       [ 11,  22,  33],
       [101, 202, 303],
       [111, 222, 333]])

Upvotes: 2

Ethan
Ethan

Reputation: 1373

Just drop the np.all()

def twoCond():
    a = np.array([[  1,   2,   3],[ 11,  22,  33],[101, 202, 303],[100, 200, 303],[111, 222, 333]])
    a = np.delete(a, np.where((a[:, 2] == 303) & (a[:,1] == 200)), axis=0)
    return a

Results:

>>> twoCond()
array([[  1,   2,   3],
       [ 11,  22,  33],
       [101, 202, 303],
       [111, 222, 333]])

Upvotes: 1

Related Questions