Python NumPy: How to Delete Rows and Columns in NumPy Arrays
NumPy's np.delete() function removes elements, rows, or columns from arrays by creating a new array without the specified indices. Understanding axis parameters is key to controlling what gets deleted.
Deleting Rows (axis=0)
Use axis=0 to delete entire rows from a 2D array:
import numpy as np
matrix = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
# Delete row at index 1 (second row)
result = np.delete(matrix, 1, axis=0)
print(result)
Output:
[[10 20 30]
[70 80 90]]
Deleting Multiple Rows
Pass a list of indices to delete several rows at once:
import numpy as np
matrix = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
])
# Delete rows 0 and 2
result = np.delete(matrix, [0, 2], axis=0)
print(result)
# Delete first and last rows
result = np.delete(matrix, [0, -1], axis=0)
print(result)
Output:
[[ 4 5 6]
[10 11 12]]
[[4 5 6]
[7 8 9]]
Deleting Columns (axis=1)
Use axis=1 to delete entire columns:
import numpy as np
matrix = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
# Delete column at index 1 (second column)
result = np.delete(matrix, 1, axis=1)
print(result)
Output:
[[10 30]
[40 60]
[70 90]]
Deleting Multiple Columns
import numpy as np
matrix = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
])
# Delete columns 0 and 2
result = np.delete(matrix, [0, 2], axis=1)
print(result)
Output:
[[ 2 4]
[ 6 8]
[10 12]]
Deleting from 1D Arrays
For 1D arrays, no axis parameter is needed:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
# Delete element at index 2
result = np.delete(arr, 2)
print(result) # [10 20 40 50]
# Delete last element
result = np.delete(arr, -1)
print(result) # [10 20 30 40]
# Delete multiple elements
result = np.delete(arr, [0, 2, 4])
print(result) # [20 40]
Output:
[10 20 40 50]
[10 20 30 40]
[20 40]
Deleting Ranges with np.s_
Use np.s_ to specify slice-based deletion:
import numpy as np
matrix = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]
])
# Delete rows 1 through 3 (indices 1, 2, 3)
result = np.delete(matrix, np.s_[1:4], axis=0)
print(result)
# Delete every other column
result = np.delete(matrix, np.s_[::2], axis=1)
print(result)
Output:
[[ 1 2 3 4]
[17 18 19 20]]
[[ 2 4]
[ 6 8]
[10 12]
[14 16]
[18 20]]
np.s_ creates slice objects, making it easy to specify ranges like [1:4], [::2], or [:-1] for deletion.
Original Array Unchanged
np.delete() returns a new array-the original remains intact:
import numpy as np
original = np.array([[1, 2], [3, 4], [5, 6]])
modified = np.delete(original, 1, axis=0)
print("Original (unchanged):")
print(original)
print("\nModified (new array):")
print(modified)
Output:
Original (unchanged):
[[1 2]
[3 4]
[5 6]]
Modified (new array):
[[1 2]
[5 6]]
If you want to modify an array "in place," reassign the result: arr = np.delete(arr, index, axis=0).
Conditional Deletion
Delete rows or columns based on conditions:
import numpy as np
matrix = np.array([
[1, 2, 3],
[4, 0, 6], # Contains zero
[7, 8, 9],
[0, 11, 12] # Contains zero
])
# Find rows containing zero
rows_with_zero = np.where(np.any(matrix == 0, axis=1))[0]
print(f"Rows with zero: {rows_with_zero}")
# Delete those rows
result = np.delete(matrix, rows_with_zero, axis=0)
print(result)
Output:
Rows with zero: [1 3]
[[1 2 3]
[7 8 9]]
Delete Rows Based on Column Values
import numpy as np
data = np.array([
[1, 100],
[2, 50],
[3, 200],
[4, 75]
])
# Delete rows where second column < 100
rows_to_delete = np.where(data[:, 1] < 100)[0]
result = np.delete(data, rows_to_delete, axis=0)
print(result)
Output:
[[ 1 100]
[ 3 200]]
Deleting from 3D Arrays
The same axis logic extends to higher dimensions:
import numpy as np
# 3D array: 2 layers, 3 rows, 4 columns
arr_3d = np.arange(24).reshape(2, 3, 4)
print(f"Original shape: {arr_3d.shape}") # (2, 3, 4)
# Delete along axis 0 (remove layer)
result = np.delete(arr_3d, 0, axis=0)
print(f"After deleting layer 0: {result.shape}") # (1, 3, 4)
# Delete along axis 1 (remove row from each layer)
result = np.delete(arr_3d, 1, axis=1)
print(f"After deleting row 1: {result.shape}") # (2, 2, 4)
# Delete along axis 2 (remove column from each layer)
result = np.delete(arr_3d, [0, 2], axis=2)
print(f"After deleting columns 0,2: {result.shape}") # (2, 3, 2)
Output:
Original shape: (2, 3, 4)
After deleting layer 0: (1, 3, 4)
After deleting row 1: (2, 2, 4)
After deleting columns 0,2: (2, 3, 2)
Alternative: Boolean Indexing
For complex conditions, boolean indexing can be cleaner:
import numpy as np
matrix = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
])
# Keep only rows where first column > 3
mask = matrix[:, 0] > 3
result = matrix[mask]
print(result)
Output:
[[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Performance Consideration
np.delete() creates a new array, which involves memory allocation and copying. For repeated deletions, consider building a mask first:
import numpy as np
matrix = np.arange(100).reshape(20, 5)
indices_to_delete = [0, 5, 10, 15]
# Less efficient: multiple delete calls
result = matrix.copy()
for i in sorted(indices_to_delete, reverse=True):
result = np.delete(result, i, axis=0)
# More efficient: single operation
mask = np.ones(len(matrix), dtype=bool)
mask[indices_to_delete] = False
result = matrix[mask]
Quick Reference
| Goal | Code |
|---|---|
| Delete single row | np.delete(arr, i, axis=0) |
| Delete single column | np.delete(arr, j, axis=1) |
| Delete multiple rows | np.delete(arr, [0, 2], axis=0) |
| Delete multiple columns | np.delete(arr, [1, 3], axis=1) |
| Delete range of rows | np.delete(arr, np.s_[1:4], axis=0) |
| Delete from 1D array | np.delete(arr, i) |
| Delete every nth element | np.delete(arr, np.s_[::n], axis) |
Summary
-
Use
np.delete(arr, indices, axis)to remove rows (axis=0) or columns (axis=1) from NumPy arrays. The function always returns a new array, leaving the original unchanged. -
For range-based deletion, use
np.s_[]to create slice objects. -
For complex conditional deletion, boolean indexing often provides a cleaner solution.