AppDividend
Latest Code Tutorials

# Python Numpy delete() Function | Numpy.delete()

Python numpy delete() is an inbuilt numpy function that is used to delete any subarray from an array along with the mentioned axis. The numpy delete() function returns the new array after performing the deletion operation. For a 1D array, it just deletes the object which we want to delete.

## Python Numpy delete()

Python numpy.delete(array, object, axis = None) method returns the new array with the deletion of sub-arrays along with the mentioned axis.

### Syntax

`numpy.delete(array, object, axis = None)`

The numpy delete() function takes 3 parameters:

1. array: This is the input array.
2. object: This can be any single number or a subarray.
3. axis: This indicates the axis which is to be deleted from the array.

### Return Value

The numpy delete() function returns the array by deleting the subarray, which was mentioned during the function call.

### Programming Example

#### Deleting elements from a 1D array

See the following code.

```# Importing numpy
import numpy as np

# We will create an 1D array

# this will create an array with values 0 to 5
arr1 = np.arange(6)
# Printing the array
print("The array is: ", arr1)

# Now we will call delete() function
# To delete the element 3
object = 3  # 3 is to be deleted

# here arr1 is the main array
# object is the number which is to be deleted
arr = np.delete(arr1, object)

# Printing new array
print("After deleting ", object, " new array is: ")
print(arr)
```

#### Output

```The array is:  [0 1 2 3 4 5]
After deleting 3 new array is:
[0 1 2 4 5]
```

#### Explanation

In this program, we have first created a 1D array using the numpy arange() function. Then we have printed the original array.

Then we have initialized the value, which is to be deleted from the 1D array in the object variable. Then, we have called the delete() function, bypassing the object in the function.

Please note that we had not mentioned axis when we called delete() function because in the 1D array, there is only one axis, so by default axis should be None.

However, in the object, we gave the value 3, so it deleted 3 from the original array, and then it returned the new array, which we have printed at last.

## Python Numpy: Delete elements from a 2D array

Using the NumPy method np.delete(), you can delete any row and column from the NumPy array ndarray. We can also remove elements from a 2D array using numpy delete() function. See the following code.

```# Importing numpy
import numpy as np

# We will create a 2D array
# Of shape 4x3
arr1 = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9), (50, 51, 52)])
# Printing the array
print("The array is: ")
print(arr1)

# Now we will call delete() function
# To delete the subarray present in array

# This indicates this will delete 3rd column
# Of the array
obj = 2  # 3rd column
axis = 1  # column wise

# here arr1 is the main array
# object is the number which is to be deleted
arr = np.delete(arr1, obj, axis)

# Printing new array
print("After deleting column wise new array is: ")
print(arr)

# Now we will delete 2nd row of the array
arr = np.delete(arr, 1, 0)

# Printing new array
print("After deleting row wise new array is: ")
print(arr)
```

#### Output

```The array is:
[[ 1  2  3]
[ 4  5  6]
[ 7  8  9]
[50 51 52]]
After deleting column wise new array is:
[[ 1  2]
[ 4  5]
[ 7  8]
[50 51]]
After deleting row wise new array is:
[[ 1  2]
[ 7  8]
[50 51]]
```

#### Explanation

In this program, we have declared a 2D numpy array of size 4×3, as we can see in the output.

At first, we wanted to delete the 3rd column of the array. For that, we have passed value 2 to obj (obj=2) as an array index starts from 0 and given axis =1, which indicates it will delete the column. Means, it will delete the 3rd column. If we give axis=0, then it will delete the 3rd row.

However, we have deleted the 3rd column and then printed the new array.

After that, we wanted to delete the 2nd row of the new array, that’s why we have passed 1 as object value and axis=0, because axis=0 indices the row, and object indicates which row to be deleted.

Finally, we have printed the final array.

## Python Numpy: Delete row

Using numpy.delete(), and we can remove an entire row from an array. See the following code.

```import numpy as np

a = np.arange(12).reshape(3, 4)
print(a, '\n')

a_del = np.delete(a, 1, 0)
print('After removing second row')
print(a_del, '\n')

print('Original Array')
print(a)
```

#### Output

``` python3 app.py
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

After removing second row
[[ 0  1  2  3]
[ 8  9 10 11]]

Original Array
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]```

From the output, we can see that 2nd row(obj=1, axis=0) is removed from the array.

The original array is not changed, and the new copy of ndarray is returned.

## Python Numpy: Specify the index for row: obj

We can specify the index (row number or column number) to be deleted in the second parameter obj. The index starts at 0. Specifying a nonexistent index raises an error.

```import numpy as np

a = np.arange(12).reshape(3, 4)
print(a, '\n')

a_del = np.delete(a, 0, 0)
print('After removing first row')
print(a_del, '\n')

a_del2 = np.delete(a, 2, 0)
print('After removing third row')
print(a_del2, '\n')

print('Original Array')
print(a)
```

#### Output

```pyt python3 app.py
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

After removing first row
[[ 4  5  6  7]
[ 8  9 10 11]]

After removing third row
[[0 1 2 3]
[4 5 6 7]]

Original Array
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]```

#### Explanation

In the above example, we have removed the first and third rows of the array. The index starts at 0. So 0 means first row, and 2 means the third row.

Let’s pass the index which does not exist in the array and see the output.

```import numpy as np

a = np.arange(12).reshape(3, 4)
print(a, '\n')

a_del = np.delete(a, 3, 0)
print('After removing fourth row')
print(a_del, '\n')

print('Original Array')
print(a)
```

#### Output

```python3 app.py
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

Traceback (most recent call last):
File "app.py", line 6, in <module>
a_del = np.delete(a, 3, 0)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/lib/function_base.py", line 4371, in delete
"size %i" % (obj, axis, N))
IndexError: index 3 is out of bounds for axis 0 with size 3```

#### Explanation

We have specified a nonexistent index; that is why it raises an error.

## Python Numpy: Specify the axis (dimension): axis

Specify an axis (dimension) to be deleted in a third parameter axis.

Axis number starts from 0. In the case of the two-dimensional array, the row is the first dimension (axis=0), and the column is a second dimension (axis=1). Specifying the nonexistent dimension raises the error.

See the following code.

```import numpy as np

a = np.arange(12).reshape(3, 4)
print(a, '\n')

a_del = np.delete(a, 1, 0)
print('After removing second row')
print(a_del, '\n')

a_del2 = np.delete(a, 1, 1)
print('After removing second column')
print(a_del2, '\n')

print('Original Array')
print(a)
```

#### Output

```python3 app.py
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

After removing second row
[[ 0  1  2  3]
[ 8  9 10 11]]

After removing second column
[[ 0  2  3]
[ 4  6  7]
[ 8 10 11]]

Original Array
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]```

Let’s define the axis which does not exist in the array and see the output.

```import numpy as np

a = np.arange(12).reshape(3, 4)
print(a, '\n')

a_del = np.delete(a, 1, 2)
print('After removing second row')
print(a_del, '\n')

print('Original Array')
print(a)
```

#### Output

```python3 app.py
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

Traceback (most recent call last):
File "app.py", line 6, in <module>
a_del = np.delete(a, 1, 2)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/lib/function_base.py", line 4300, in delete
axis = normalize_axis_index(axis, ndim)
numpy.AxisError: axis 2 is out of bounds for array of dimension 2```

So, Specifying a nonexistent dimension raises an error.

## Python Numpy: Delete multiple rows and columns

Multiple rows and columns can be removed at once by specifying the list or a slice in the second parameter obj. We can delete multiple rows and columns at once using the following way.

1. Use a Python list
2. Use a Python slice
3. Use a np.s_[]

### Use a Python list

Specify the row numbers and column numbers in the form of a list to be deleted in an array.

See the following code.

```import numpy as np

a = np.arange(12).reshape(3, 4)
print(a, '\n')

a_del = np.delete(a, [0, 3], 1)
print('After removing first and fourth columns')
print(a_del, '\n')

print('Original Array')
print(a)
```

#### Output

```python3 app.py
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

After removing first and fourth columns
[[ 1  2]
[ 5  6]
[ 9 10]]

Original Array
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]```

If we want to remove the column, then we have to pass 1 in np.delete(a, [0, 3], 1) function, and we need to remove the first and fourth column from the array. So we have written np.delete(a, [0, 3], 1) code.

We can also remove multiple rows at once. See the following code.

```import numpy as np

a = np.arange(12).reshape(3, 4)
print(a, '\n')

a_del = np.delete(a, [1, 2], 0)
print('After removing second and third rows')
print(a_del, '\n')

print('Original Array')
print(a)
```

#### Output

```python3 app.py
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

After removing second and third rows
[[0 1 2 3]]

Original Array
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]```

In the above example, we have removed the second and third rows from the array.

`np.delete(a, [1, 2], 0)`

In the above code, we have passed 0, which means we need to remove row or x-axis.

Then we have specified the second and third row to be removed. The array index starts from 0.

### Use a Python slice

It is also possible to specify the multiple rows and columns by using the slice specifying a range with [start: stop: step].

Create a slice object with a slice() and specify it as a second parameter obj.

It is equivalent to [: stop] if there is single argument, [start: stop] if there are two arguments, and [start: stop: step] if there are three arguments. If you want to omit, specify None explicitly.

See the following code.

```import numpy as np

a = np.arange(12).reshape(3, 4)
print(a, '\n')

print('After removing first and second columns')
a_del = np.delete(a, slice(2), 1)
print(a_del, '\n')

print('Original Array')
print(a)
```

#### Output

```python3 app.py
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

After removing first and second columns
[[ 2  3]
[ 6  7]
[10 11]]

Original Array
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]```

### Use a np.s_[]

Use numpy.s_[] if you want to write in a form [start:stop:step].

Let’s remove the first and second columns using np.s_[] function. Remember array index starts from 0.

See the following code.

```import numpy as np

a = np.arange(12).reshape(3, 4)
print(a, '\n')

print('After removing first and second columns')
a_del = np.delete(a, np.s_[:2], 1)
print(a_del, '\n')

print('Original Array')
print(a)
```

#### Output

```python3 app.py
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

After removing first and second columns
[[ 2  3]
[ 6  7]
[10 11]]

Original Array
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]```

## Conclusion

Python numpy delete() function is used to remove a single row or column, multiple rows, or columns in the array. We can also use Python slice and list to remove multiple elements(rows and columns). We have seen many variations of it in this tutorial. I hope you learn many things from this brief example.

This site uses Akismet to reduce spam. Learn how your comment data is processed.