# How to select columns from NumPy array

In this post, we are going to understand how to select columns from NumPy array, N-Dimensional Numpy array contains rows and columns, We can filter data by selecting columns or rows. To select data by column from NumPy array slicing or Ellipsis or np. r_[] method is used. To run this program NumPy library must be installed on the local system

### 1. How to select columns from NumPy array using slicing

The syntax to select the column using slicing is given below, where the colon(:) represents that we are selecting all rows and passing column index or position to select the correspondence column.

#### Syntax

```numpyarry[  : ,column]
```

#### Parameters

• column:The column represent column index or position for whatever column we have to select.
• Colon(:) :We are selecting all rows.

#### How does it work

• We have created a NumPy array of size 12 and distributed it into 3 rows and 4 columns.
• Selecting the 3rd column by using [:, 2]
• Selecting the 2nd column by using [:, 1]
• Selecting first column by using [:, 0]

### Python program to select columns from NumPy array

```import numpy as np

origanlArr = np.arange(12).reshape(3,4)

print('Original array:\n',origanlArr)

Third_Col = origanlArr[:, 2]
Sec_Col = origanlArr[:, 1]
Frist_Col = origanlArr[:, 0]

print('\nThird col:',Third_Col)
print('Second col:',Sec_Col)
print('First col:',Frist_Col )
```

Output

```Original array:
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

Third col: [ 2  6 10]
Second col: [1 5 9]
First col: [0 4 8]
```

### 2. How to select columns from NumPy array Using Ellipsis

The Ellipsis works similar to slicing instead of colon(:) we are using Ellipsis three-dot(. . .) to select all rows and column position or index to select column. Let us understand with the below example.

#### Syntax

``` numparray[…,column]
```

#### Parameters

• column: The column represents the column index or position for whatever column we have to select.
• Ellipsis (…) : We are selecting all rows.

#### How does it works

• We have created an numpy array of size 12 and distributed it into 3 rows and 4 columns.
• Selecting the 3rd column by using […, 2]
• Selecting the 2nd column by using […, 1]
• Selecting first column by using […, 0]
```import numpy as np

origanlArr = np.arange(12).reshape(3,4)

print('Original array:\n',origanlArr)

Third_Col = origanlArr[..., 2]
Sec_Col = origanlArr[..., 1]
Frist_Col = origanlArr[..., 0]

print('\nThird col:',Third_Col)
print('Second col:',Sec_Col)
print('First col:',Frist_Col )

```

Output

```Original array:
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

Third col: [ 2  6 10]
Second col: [1 5 9]
First col: [0 4 8]
```

### 3. Using np.r_ [] to select range of columns from NumPy array

We can select the range of columns by using the np.r_ () function Translates slice objects to concatenation along the first axis. If we use slicing notation[start:stop:step] it works like the np.arange() function.In this example, we are using the np. r_[] to select the range of columns from the NumPy array.

• We have created an array of size 12 and distributed it 3 rows and 4 columns.
• Finally using the np.r_[] function to select the range of columns from the NumPy array.

```import numpy as np

origanlArr = np.arange(12).reshape(3,4)

print('Original array:\n',origanlArr)

print(origanlArr[:, np.r_[0:1, 3]])
```

Output

```Original array:
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

Selcted columns :
[[ 0  3]
[ 4  7]
[ 8 11]]
```

### 4. np.r_[] to Select a range of columns from NumPy array

In this example, we are selecting all rows and range of columns from a NumPy array by using the np. r_[] function.We have followed these step

• We have created an array of size 12 and distributed it in 3 rows and 4 columns.
• Finally using the np.r_[] function to select the range of columns from the NumPy array.
```import numpy as np

origanlArr = np.arange(12).reshape(3,4)

print('Original array:\n',origanlArr)

print('\n Selcted columns :\n',origanlArr[:, np.r_[:1, 3,1:2]])
```

Output

```Original array:
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

Selcted columns :
[[ 0  3  1]
[ 4  7  5]
[ 8 11  9]]
```

### Summary

In this post, we have learned how to select columns from NumPy array by using the different built-in methods with examples