user424060
user424060

Reputation: 1581

accumulate numpy array for just one column

I have a NumPy array, i want to accumulate the values of one column, say the 2nd column.

a = np.array([[1,2],[2,4]])
# some kind of accumulate function that accumulates just one column:
np.add.accumulate(a, 2)

a should now be [[1,2],[2,6]]

Is there a way to do this in NumPy?

Upvotes: 1

Views: 1161

Answers (1)

Paul
Paul

Reputation: 43680

a = np.array([[1,2],[2,4]])
np.add.accumulate(a[:,1], out=a[:,1])

a is now:

array([[1, 2],
       [2, 6]])

Upvotes: 2

Related Questions