S.AMEEN
S.AMEEN

Reputation: 1521

How to modify N columns of numpy array at the same time?

How to modify N columns of numpy array?? For example, I have a numpy array as follows:

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

How do I change the elements of first, second and forth columns of P?

Upvotes: 2

Views: 95

Answers (1)

Kasravnd
Kasravnd

Reputation: 107347

Use indexing:

Here is an example:

>>> P[:, [0, 1, 3]] += 10
>>> 
>>> P
array([[11, 12,  3, 18,  6],
       [14, 15,  6, 14,  5],
       [10,  8,  5, 13,  0]])

Upvotes: 3

Related Questions