madschm
madschm

Reputation: 39

How to add a value to every element in a specified column of a 2D array?

I have a matrix, a:

a = np.random.randn(10,7)
a = a.astype(int)

How do I add 2 to every element in a specified column, let's say column 3?

I have tried a few approaches such as result = [x+2 for x in a] , but that doesn't work for 2D arrays because I don't know how to specify a column, and result = np.add(a(:,3(i+2))) which obviously gives me invalid syntax.

Thanks.

Upvotes: 0

Views: 713

Answers (1)

Michał Dramiński
Michał Dramiński

Reputation: 33

Try this:

a = np.random.randn(10, 7)
a = a.astype(int)
print(a)
a[:, 2] += 2
print(a)

Upvotes: 1

Related Questions