Modify a particular row/column of a NumPy array

Rows and columns of NumPy arrays can be selected or modified using the square-bracket indexing notation in Python.

To select a row in a 2D array, use P[i]. For example, P[0] will return the first row of P.

To select a column, use P[:, i]. The : essentially means “select all rows”. For example, P[:, 1] will select all rows from the second column of P.

If you want to change the values of a row or column of an array, you can assign it to a new list (or array) of values of the same length.

To change the values in the first row, write:

>>> P[0] = [7, 8, 9]
>>> P
array([[7, 8, 9],
       [4, 5, 6]])

To change the values in the second column, write:

>>> P[:, 1] = [7, 8]
>>> P
array([[1, 7, 3],
       [4, 8, 6]])

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)