In this python numpy program, we will calculate the sum of all columns of the NumPy array.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Calculate the sum of all columns of the given array using sum() and set axis equal to 0.
- Print the output.
import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])
print("Sum of all columns: ",x.sum(axis=0))
Output :
Sum of all columns: [23 22 10]