43

How do I modify particular a row or column of a NumPy array?

For example I have a NumPy array as follows:

P = array([[1, 2, 3],
           [4, 5, 6]])

How do I change the elements of first row, [1, 2, 3], to [7, 8, 9] so that the P will become:

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

Similarly, how do I change second column values, [2, 5], to [7, 8]?

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

3 Answers 3

63

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]])
2
  • You said above: "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." But is it OK to just alter the source array without copying it? I have an array, a, and then I just do a[: ,1] = [1, 2, 3] and that alters my source array. Are there any issues in just altering the source in place without copying, etc.or is that OK practice?
    – Matt M.
    Mar 27, 2020 at 19:28
  • 1
    @mbird: it's fine to modify an array without copying it, as long as you are happy for any other array that shares the same underlying data as a to potentially be modified too. For example if you set b = a[0] and then set a[: ,1] = [1, 2, 3], the change of values would also affect b. (Often this is the desired outcome, but not always.)
    – Alex Riley
    Mar 27, 2020 at 21:29
5

In a similar way if you want to select only two last columns for example but all rows you can use:

print P[:,1:3]
1
  • 3
    This should be a comment to the above answer.
    – LoMaPh
    Nov 10, 2017 at 2:57
2

If you have lots of elements in a column:

import numpy as np
np_mat = np.array([[1, 2, 2],
                   [3, 4, 5],
                   [5, 6, 5]])
np_mat[:,2] = np_mat[:,2] * 3
print(np_mat)

It is making a multiplied by 3 change in third column:

    [[ 1  2  6]
     [ 3  4 15]
     [ 5  6 15]]
3
  • 1
    This answer doesn't address the OP's question, regarding changing specific rows/columns of the array.
    – dspencer
    Apr 9, 2020 at 2:35
  • what do you mean it's obviously special column! and it's a solution for bigger matrix that you can't add one by one. Apr 9, 2020 at 10:10
  • 1
    that helped to know how to update all values of a particular column. Mar 15, 2022 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.