# Delete elements by value or condition in NumPy array

In this post, We are how to delete elements by value or condition in NumPy array by using the numpy library function np. delete().We will cover the delete element by value, delete element by index, delete element by mutiple condition, delete rows by mutiple condition, delete row in place from numpy array with examples.

### 1. NumPy array Remove elements by value

In this NumPy array, We are removing all occurrences of element 12 by using the condition myarr!=12. this condition returns a boolean array True at the place where the value is not 12 and False at another place.

The boolean array we have passed to numpy operator [] selects the element that has true at given indexes.

```import numpy as np

myarr = np.array([[12,14,70,80],[12,75,60,50],[3,6,9,12],[4,8,12,16]])

myarr = myarr[myarr != 12]

print([myarr != 12])

print(myarr)

```

Output

```Boolean array:
[array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
True,  True,  True])]
[14 70 80 75 60 50  3  6  9  4  8 16]
```

### 2. NumPy delete element by indexes

In this example we are numpy.delete() function to delete given indexes[3,6,9] from numpy array. It will return a numpy array after deleting the given indexes.n this Python code example, we are passing rows indexes as a list to numpy.delete() function as parameter.

• The first parameterin numpy.delete() function is numpy array
• The second parameters is the list of rows indexes that mean it will delete all rows at given indexes.
```import numpy as np

myarr = np.array([[12,14,70,80],[12,75,60,50],[3,6,9,12],[4,8,12,16]])

index = [3, 6, 9]

myarr = np.delete(myarr, index)

print(myarr)

```

Output

```[12 14 70 12 75 50  3  9 12  4  8 12 16]
```

### 3. Remove element by mutiple condition

In this python program example, we are deleting elements that are greater than 12 and less than =50. First, we have created a boolean array bypassing multiple conditions to the [] operator. It selects the element that satisfies a given condition from the boolean array.

```import numpy as np

myarr = np.array([[12,14,70,80],[12,75,60,50],[3,6,9,12],[4,8,12,16]])

myarr = myarr[ (myarr >= 12) & (myarr <= 50) ]

print(myarr)

```

Output

```[12 14 12 50 12 12 16]
```

### argwhere(),np.delete() to delete element by mutiple condition

We can delete elements from the numpy array by using mutiple conditions by using np. anywhere() and np. delete() as we doing in the below example.

```import numpy as np

myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])

newarr = np.delete(myarr, np.argwhere( (myarr >= 3) & (myarr <= 12) ))

print('new array1:\n',newarr)
```

Output

```new array1:
[20 75 60 50  3  6  9 12  4  8 12 16]
```

### 4. NumPy array delete row

In this Python code example, we are passing rows to numpy.delete() function as parameter.

• The first parameter in numpy.delete() function is numpy array
• The second parameters is row index.
• The third parameter is axis =0 that means it will delete first row.
```import numpy as np

myarr = np.array([[12,14,70,80],[12,75,60,50],[3,6,9,12],[4,8,12,16]])

myarr = np.delete(myarr, (0), axis=0)

print(myarr)

```

Output

```[[12 75 60 50]
[ 3  6  9 12]
[ 4  8 12 16]]
```

### 5. Delete mutiple rows by indexes using slicing

We can delete multiple rows and columns from the numpy array using numpy.delete() function using slicing.The first argument is numpy array the second argument is slicing and the third argument is the axis =0 means, which will delete multiple rows.

• slice[:stop] : if we pass only one argument to slice()
• slice[start:stop] : if we pass two argument to slice() function
• slice[start:stop:step] : if we pass three arguments to slice() function
```import numpy as np

myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])

newarr = np.delete(myarr, slice(2), 0)

print('new array1:\n',newarr)

```

Output

```new array1:
[[ 3  6  9 12]
[ 4  8 12 16]]
```

### 5. Numpy array Delete multiple columns using slicing

We can delete multiple rows and columns from the numpy array using numpy.delete() function using slicing.The first argument is numpy array the second argument is slicing and the third argument is the axis =1 means, which will delete multiple columns.

• slice[:stop] : if we pass only one argument to slice()
• slice[start:stop] : if we pass two argument to slice() function
• slice[start:stop:step] : if we pass three arguments to slice() function
```import numpy as np

myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])

newarr = np.delete(myarr, slice(2), 0)

print('new array1:\n',newarr)

```

Output

```new array1:
[[70 80]
[60 50]
[ 9 12]
[12 16]]
```

### 6. NumPy array delete row in-place

In this example, we are deleting row at index 1 in-place using the numpy.delete() function by passing the third argument as 0.

```import numpy as np

myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])

newarr = np.delete(myarr, 1, 0)
print('new array1:\n',newarr)

```

Output

```new array1:
[[12 14 70 80]
[ 3  6  9 12]
[ 4  8 12 16]]
```

### Summary

In this post, we have learned mutiple ways of how to delete elements by value or condition in NumPy array by using numpy library function np. delete() with examples