Extract Specific RANGE of columns in numpy array Python Extract Specific RANGE of columns in numpy array Python arrays arrays

Extract Specific RANGE of columns in numpy array Python


You can just use e[:, 1:5] to retrive what you want.

In [1]: import numpy as npIn [2]: e = np.array([[ 0,  1,  2,  3, 5, 6, 7, 8],   ...:               [ 4,  5,  6,  7, 5, 3, 2, 5],   ...:               [ 8,  9, 10, 11, 4, 5, 3, 5]])In [3]: e[:, 1:5]Out[3]:array([[ 1,  2,  3,  5],       [ 5,  6,  7,  5],       [ 9, 10, 11,  4]])

https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html


Numpy row and column indices start counting at 0.

The rows are specified first and then the column with a comma to separate the row from column.

The ":" (colon) is used to shortcut all rows or all columns when it is used alone.

When the row or column specifier has a range, then the ":" is paired with numbers that specify the inclusive start range and the exclusive end range.

For example

import numpy as npnp_array = np.array( [ [ 1, 2, 3, ],                       [ 4, 5, 6, ],                       [ 7, 8, 9  ] ]  )first_row = np_array[0,:]first_rowoutput: array([1, 2, 3])last_column = np_array[:,2]last_columnoutput: array([3, 6, 9])first_two_vals = np_array[0,0:2]first_two_valsoutput: array([1, 2])