# Numpy array delete row by mutiple condition

In this post, we are going to learn how to delete rows by multiple conditions in a numpy array. We are going to use np. delete() and np.where() function or np.any() or np.all() function with examples.

### NumPy delete function

The numpy. delete() function returns a new array after deleting rows and columns along with the given axis. It takes three arguments as given below

#### Parameters

• array: The array in which rows and columns to be deleted
• obj: index position, list of indexes position or slice index to be delete from numpy array
• axis: row or column along which to delete values from numpy arraydefulat value is 0.
• axis =0 : it deletes the rows
• axis=1 : it deletes the colums
• numpy. where() : It will operates on numpy array based on given condition as per given axis.

### 1. Numpy array delete row by mutiple condition

The numpy. delete() method delete the rows and columns as per the given axis, We have passed axis=0 to delete rows and condition by using np.where() function.To delete the columns by condition we can pass axis =1.

• It delete row that contain any element which are greater and equal to 5 and element <=20
• The rows 1st,2nd,3rd contain the element that satisfy the passed condition.
• So the row 0 is returns after deleteing row from numpy array.

```import numpy as np

myarr = np.arange(25).reshape((5, 5))

print(myarr)

myarr = np.delete(myarr, np.where((myarr >= 5) & (myarr <= 20)), axis=0)

print('The result array:\n',myarr)
```

Output

```Original Array:
[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]

The result array:
[[0 1 2 3 4]]
```

### 2. Numpy array delete row by mutiple condition

In this python program example we are deleting rows by multiple conditions which first element is greater than 3 and less than =12 by passing condition by using np.where() method and result return by np where is used by np.delete() method to delete rows

```import numpy as np

myarr = np.arange(25).reshape((5, 5))

myarr = np.delete(myarr, np.where(
(myarr[:, 0] >= 3) & (myarr[:, 0] <=12 )), axis=0)

print('The result array:\n',myarr)

```

Output

```Original Array:
[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]

The result array:
[[ 0  1  2  3  4]
[15 16 17 18 19]
[20 21 22 23 24]]
```

### 3. NumPy.any() to delete any rows by multiple condition

In this example we are using the np.any() methd to delete the numpy array any rows has any element is 5 or 12.So the row 1st,3rd and 4th rows is deleted.

```import numpy as np

myarr = np.arange(25).reshape((5, 5))

print(myarr)
print('\nAfter deleting row on mutiple condition\n',myarr[np.any((myarr == 5) | (myarr == 12), axis=0)])

```

Output

```Original Array:
[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]

After deleting row on mutiple condition
[[ 0  1  2  3  4]
[10 11 12 13 14]]
```

### 4. NumPy.all() to delete rows on mutiple condition

The numpy.all() function will return true if all element of numpy array passes the given condition and else return False.

• We have created a numpy array using of size(5,5).
• The np.all() function return an numpy array of elements that satisfy the condition.
• Finally printing the result array using the print() function
```import numpy as np

myarr = np.arange(25).reshape((5, 5))

print(myarr)

print('result array:',myarr[1, ~np.all(myarr<=5, axis=0)])
```

Output

```Original Array:
[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]

Result array:
[5 6 7 8 9]
```