To sum over columns in Python, you can use the NumPy library which provides efficient array manipulation. Here's how you can do it:
main.py202 chars12 lines
In this example, we have a 2D array with 3 columns. By using np.sum
, we specify axis=0
to perform the summation operation along each column. The resulting column sums are stored in the column_sums
variable.
Output:
main.py11 chars2 lines
Thus, the sum of the first column is 12, sum of the second column is 15, and sum of the third column is 18.
gistlibby LogSnag