Let’s learn how to get column in Numpy array. This is what gives you possibily to manipulate data in Numpy Python library.

We have such Numpy array:

[1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10], [2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10], [3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10]

How to get only elements from fourth column? Like that:

import numpy as np my_array = np.array(( [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10], [2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10], [3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10])) chosen_elements = my_array[:, 3] print(chosen_elements)

chosen_elements = my_array[:, 3] means to return every (colon) element from fourth column (3 means fourth column because computers start counting from 0).

Keep moving further.

How to get element from second row and fourth column?

import numpy as np my_array = np.array(( [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10], [2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10], [3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10])) chosen_elements = my_array[1, 3] print(chosen_elements)

chosen_elements = my_array[1, 3] returned second row and fourth column.

How to get many columns then? Let’s get columns second to sixth.

import numpy as np my_array = np.array(( [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10], [2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10], [3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10])) chosen_elements = my_array[:, 1:6] print(chosen_elements)

chosen_elements = my_array[:, 1:6] is returning columns second to sixth thanks to colon. Colon means range 1 to 6 to columns 2 to 6.

One more tip.

How to get many columns with a step? Let’s return column second to sixth but every second column.

import numpy as np my_array = np.array(( [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10], [2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10], [3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10])) chosen_elements = my_array[:, 1:6:2] print(chosen_elements)

chosen_elements = my_array[:, 1:6:2] as you can notice added a step. Another colon is doing that and digit 2 tells how big step is.

Now you can get columns in Numpy arrays.