How to calculate the sum of columns and rows in the Numpy Python library? Let’s find out in the python tutorial below.
In Numpy, you can quickly sum columns and rows of your array.
Example of numpy sum
To calculate the sum of array columns, just add the 0 parameter.
import numpy as np my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(my_array) print("Sum of columns equals: ", np.sum(my_array, 0))
Similarly, to sum up rows, add 1 as a parameter.
import numpy as np my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(my_array) print("Sum of rows equals: ", np.sum(my_array, 1))
To sum up every array element, just remove the parameters.
import numpy as np my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(my_array) print("Sum of every elements of your array equals: ", np.sum(my_array))