user17855079
user17855079

Reputation:

How do I sum all the columns in 2D matrix in Python?

I used the following code to sum all the rows in a 2D matrix but I want to sum all the columns instead:

row_sum = sum(map(sum,[arr]))

Upvotes: 0

Views: 105

Answers (1)

Viettel Solutions
Viettel Solutions

Reputation: 1489

You can try the code below:

import numpy as np
arr: <2D matrix> 
col_sum = np.sum(arr, axis=1, keepdims=True)

Upvotes: 1

Related Questions