# np.delete() function in Python [4 Examples]

In this NumPy article, I will explain what the np.delete() function is in Python, covering its syntax, parameters, and practical use cases.

To understand the np.delete() function in Python, consider its flexibility in removing elements from arrays. For instance, in a 1D array like [1, 2, 3, 4, 5], using np.delete(arr, 2) removes the element at index 2, resulting in [1, 2, 4, 5]. In a 2D array, such as [[1, 2, 3], [4, 5, 6], [7, 8, 9]], np.delete(arr_2d, 1, axis=0) removes the second row, while np.delete(arr_2d, 1, axis=1) eliminates the second column.

## np.delete() function in Python

The np.delete() function in Python is used to remove elements or slices from an array. We provide the array, the indices of elements or slices to delete, and optionally the axis along which to delete (0 for rows, 1 for columns). It doesn’t modify the original array but returns a new array with the specified elements removed.

### np.delete Python syntax

The basic syntax of np.delete() function in Python is:

``numpy.delete(arr, obj, axis=None)``

Here,

### np.delete() functions return value

The np.delete() function in Python returns a new array with the specified elements removed.

Note: The original array is not modified; np.delete() creates a new array.

## np.delete() function in Python use cases

Let’s see some demonstrative examples of the np.delete() function in Python:

### 1. NumPy remove element by index

To remove values from a NumPy array by index, we can use the np.delete() function, with any special argument.

``````import numpy as np

temperatures = np.array([72, 74, 71, 70, 68, 69, 73])
updated_temperatures = np.delete(temperatures, [2, 4])
print(updated_temperatures)``````

Output:

``[72 74 70 69 73]``

After executing the code in Pycharm, one can see the output in the below screenshot.

### 2. NumPy Delete Elements from a 1D Array

Here, we will try to remove multiple values from an array in Python.

``````import numpy as np

Population = np.array([40, 29, 21, 19, 12])
new_arr = np.delete(Population, [1,3])
print(new_arr)``````

Output:

``[40 21 12]``

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

### 3. Remove values from NumPy array 2D

#### Case 1: NumPy delete row in Python

``````import numpy as np

rainfall_data = np.array([
[39.2, 40.8, 36.5],  # City 1
[49.9, 52.8, 48.7],  # City 2
[34.8, 35.4, 33.2],  # City 3
[42.4, 44.1, 41.6],  # City 4
[50.2, 51.5, 49.9],  # City 5
])
result = np.delete(rainfall_data, 1, axis=0)
print(result)``````

Output:

``````[[39.2 40.8 36.5]
[34.8 35.4 33.2]
[42.4 44.1 41.6]
[50.2 51.5 49.9]]``````

After the implementation of the code in the Pycharm editor, the screenshot is mentioned below.

#### Case 2: NumPy remove last column in Python

``````import numpy as np

state_gdps = np.array([
[2.1, 1.0, 0.9],
[1.5, 2.3, 1.1],
[1.2, 1.4, 1.6]
])
result = np.delete(state_gdps, 1, axis=1)
print(result)``````

Output:

``````[[2.1 0.9]
[1.5 1.1]
[1.2 1.6]]``````

After executing the code in Pycharm, one can see the output in the below screenshot.

## Conclusion

Here, I have explained what the np.delete() function in Python considering different use cases like deleting a value, multiple values, a full row, or a column from a 2D array.

This can help one to better understand the function of the NumPy library in Python.

You may also like to read: