+2 votes
in Programming Languages by (73.8k points)
How can I delete a row or column of a 2D array? I want to remove all elements of the row or column of the array.

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

You can use the delete() function of Numpy. To delete a row, you need to set axis=0 and to delete a column, you need to set axis=1.

Here is an example:

>>> aa=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> aa
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

>>> np.delete(aa,1,axis=0)
array([[1, 2, 3],
       [7, 8, 9]])
>>> aa
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

>>> np.delete(aa,1,axis=1)
array([[1, 3],
       [4, 6],
       [7, 9]])


...