+2 votes
in Programming Languages by (12.5k 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

0 votes
by (22.3k points)

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]])

...