Numpy: Delete row or column from an array

31 July 2020

Code

# Import library
import numpy as np

# Create array
x = np.array([[1,2,3],
              [4,5,6],
              [7,8,9]])

# Delete a column by index
xc = np.delete(arr=x, obj=1, axis=1) # obj= column index

# Delete a row by index
xr = np.delete(arr=x, obj=1, axis=0) # obj=row index


# Output
print("Original array: \n", x, '\n')
print("Deleted a column: \n", xc, '\n')
print("Deleted a row: \n", xr, '\n')

Output

Original array: 
 [[1 2 3]
 [4 5 6]
 [7 8 9]] 

Deleted a column: 
 [[1 3]
 [4 6]
 [7 9]] 

Deleted a row: 
 [[1 2 3]
 [7 8 9]] 






Any errors in code above?
Please send a message.