# NumPy sum Specific columns

In this post, We are going to learn how to calculate the NumPy sum of Specific columns with examples of sum of numpy specific columns from a list of indices, the sum of a specific range of columns, numpy sum first column,numpy second column. Numpy sum of the last column, NumPy sum based on condition. We run all below program numpy must be installed on the system.

### 1. NumPy sum Specific columns

In this Python program example, we are learning how to sum up numpy specific columns. We are summing the first column of the numpy array by using the sum() function of the Python numpy library that is imported in our program using “import numpy as np”

```import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])

columnSum = samplearray[:, 1].sum()

print('\nsum of specific colums:',columnSum)

```

Output

```sum of specific colums: 21
```

### 2. NumPy sum Specific columns Second

In this Python program example, we are summing the second column of the numpy array by using the sum() function.

```import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])

columnSum = samplearray[:,2].sum()

print('\nsum of specific colums:',columnSum)
```

Output

```sum of specific colums: 24
```

### 3.Sum of range of specific columns

In this Python program example, We will learn how to sum a range of specific columns of a numpy array using the sum() function of the numpy library.

```import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])

columnSum = samplearray[:,1:3].sum()

print('\nsum of specific colums:',columnSum)

```

Output

```sum of specific colums: 45
```

### 4. NumPy array sum last columns

In this Python program, we are finding the sum of the last column of the numpy array to sum the specific column we use slicing [:,-1] which includes all rows, and last column that means sum all elements the last column rows those are 5,10,15 so the sum is:30.

```import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])

columnSum = samplearray[:,-1].sum()
print('\nsum of last colums:',columnSum)

```

Output

```sum of specific colums: 30
```

### 5. NumPy sum based on condition

In this Python program, we are sum the elements of the numpy array based on the condition that means sum elements are less than 10 along with axis = 0. We can change the axis as per need.

```import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])

columnSum = np.sum(samplearray < 10,axis=0)
print('\nsum based on condition:',columnSum)
```

Output

```sum based on condition:: [2 2 2 2 1]
```

### 6.NumPy Sum based on list of indices

Sometimes we have indices of numpy array as list and need to find the sum based on this list of indices. In this Python program, We are finding the sum of the numy array based on the given list of indices.

```import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])

indices = [0, 2]

columnSum = samplearray[indices].sum()
print('\nnumpy array sum based on list of indices:',columnSum)
```

Output

```numpy array sum based on list of indices: 80
```

### 7. NumPy sum of specific rows and columns

Sometimes we have the need to sum specific rows and columns of numpy array as we are doing on the below python program.

```import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])
columnSum = np.sum(samplearray[1:4])
print('\nsum of numpy array:',columnSum)
```

Output

```sum of numpy array: 105
```

### Summary

In this post, we have learned how to the NumPy sum Specific columns with different program examples that includes sum numpy specific column based on condition,numpy sum list of indices,numpy sum multiple columns,numpy sum specific rows, and columns.