It looks like you just need a basic integer array indexing:

filter_indices = [1,3,5]
np.array([11,13,155,22,0xff,32,56,88])[filter_indices] 
Answer from Joran Beasley on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.4 Manual
Basic slicing occurs when obj is a slice object (constructed by start:stop:step notation inside of brackets), an integer, or a tuple of slice objects and integers. Ellipsis and newaxis objects can be interspersed with these as well. The simplest case of indexing with N integers returns an array scalar representing the corresponding item. As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_slicing.asp
NumPy Array Slicing
Slice from the index 3 from the end to index 1 from the end: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[-3:-1]) Try it Yourself » · Use the step value to determine the step of the slicing:
🌐
Pythoninformer
pythoninformer.com › python-libraries › numpy › index-and-slice
PythonInformer - Indexing and slicing numpy arrays
The index returns an element of the array, the slice returns a list of one element. Slicing a 1D numpy array is almost exactly the same as slicing a list:
🌐
Stack Overflow
stackoverflow.com › questions › 66319045 › in-numpy-how-do-i-use-a-list-of-indices-to-slice-from-the-second-dimension-of-a
python - In numpy, how do I use a list of indices to slice from the second dimension of a 2D array? - Stack Overflow
Best explained with an example. The following code gives me the result that I want, but I want to avoid the iteration/list comprehension. import numpy as np foo = np.array([range(100, 105), range(200, 205), range(300, 305)]) print("Data:\n", foo) # "Column 1 of row 0, column 3 of row 1, ..." indices = list(zip(range(len(foo)), np.array([1, 3, 4]))) print("Indices to select from foo:\n", indices) # This works, but surely there's a better way?
Top answer
1 of 2
8

Use slicing:

>>> arr = np.array([['1A34', 'RBP', 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
       ['1A9N', 'RBP', 0.0456267, 0.0539268, 0.331932, 0.0464031,
        4.41336e-06, 0.522107],
       ['1AQ3', 'RBP', 0.0444479, 0.201112, 0.268581, 0.0049757,
        1.28505e-12, 0.480883],
       ['1AQ4', 'RBP', 0.0177232, 0.363746, 0.308995, 0.00169861, 0.0,
        0.307837]], dtype=object)
>>> arr[:,:5:2]
array([['1A34', 0.0, 0.0],
       ['1A9N', 0.0456267, 0.331932],
       ['1AQ3', 0.0444479, 0.268581],
       ['1AQ4', 0.0177232, 0.308995]], dtype=object)

If the column indices are irregular then you can do something like this:

>>> indices = [0, 3, 4]
>>> arr[:, indices]
array([['1A34', 1.0, 0.0],
       ['1A9N', 0.0539268, 0.331932],
       ['1AQ3', 0.201112, 0.268581],
       ['1AQ4', 0.363746, 0.308995]], dtype=object)

Note that there's a subtle but substantial difference between slicing (which is basic indexing) and using a sequence for indexing (also known as advanced indexing or fancy indexing). When using a slice such as arr[:, :5:2], no data is copied, and we get a view of the original array. This implies that mutating the result of arr[:, :5:2] will affect arr itself. With fancy indexing arr[:, [0, 3, 4]] is guaranteed to be a copy: this takes up more memory, and mutating this result will not affect arr.

2 of 2
0

You can access the columns of a numpy array in the following way:

array[:,column_number]

To get the array of specific columns you can do as follows:

z = array([[['1A34', 'RBP', 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
   ['1A9N', 'RBP', 0.0456267, 0.0539268, 0.331932, 0.0464031,
    4.41336e-06, 0.522107],
   ['1AQ3', 'RBP', 0.0444479, 0.201112, 0.268581, 0.0049757,
    1.28505e-12, 0.480883],
   ['1AQ4', 'RBP', 0.0177232, 0.363746, 0.308995, 0.00169861, 0.0,
    0.307837]], dtype=object]) #your array here

op_array = array([ [z:,0], z[:,2], z[:,3] ])

The op_array will have the 0th, 2nd and 3rd columns as rows.

So you need to transpose it to get the output array in the desired format.

op_array.transpose()

op_array will now look as below:

op_array([['1A34', 0.0,  0.0],
       ['1A9N', 0.0456267,  0.331932],
       ['1AQ3', 0.0444479, 0.268581],
       ['1AQ4', 0.0177232,  0.308995])
Top answer
1 of 3
9

Numpy uses multiple indexing, so instead of A[1][2][3], you can--and should--use A[1,2,3].

You might then think you could do A[:, second, third], but the numpy indices are broadcast, and broadcasting second and third (two one-dimensional sequences) ends up being the numpy equivalent of zip, so the result has shape (5, 2).

What you really want is to index with, in effect, the outer product of second and third. You can do this with broadcasting by making one of them, say second into a two-dimensional array with shape (2,1). Then the shape that results from broadcasting second and third together is (2,2).

For example:

In [8]: import numpy as np

In [9]: a = np.arange(125).reshape(5,5,5)

In [10]: second = [1,2]

In [11]: third = [3,4]

In [12]: s = a[:, np.array(second).reshape(-1,1), third]

In [13]: s.shape
Out[13]: (5, 2, 2)

Note that, in this specific example, the values in second and third are sequential. If that is typical, you can simply use slices:

In [14]: s2 = a[:, 1:3, 3:5]

In [15]: s2.shape
Out[15]: (5, 2, 2)

In [16]: np.all(s == s2)
Out[16]: True

There are a couple very important difference in those two methods.

  • The first method would also work with indices that are not equivalent to slices. For example, it would work if second = [0, 2, 3]. (Sometimes you'll see this style of indexing referred to as "fancy indexing".)
  • In the first method (using broadcasting and "fancy indexing"), the data is a copy of the original array. In the second method (using only slices), the array s2 is a view into the same block of memory used by a. An in-place change in one will change them both.
2 of 3
5

One way would be to use np.ix_:

>>> out = A[np.ix_(range(A.shape[0]),second, third)]
>>> out.shape
(5, 2, 2)
>>> manual = [A[i,j,k] for i in range(5) for j in second for k in third]
>>> (out.ravel() == manual).all()
True

Downside is that you have to specify the missing coordinate ranges explicitly, but you could wrap that into a function.

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-slicing-and-indexing
Basic Slicing and Advanced Indexing in NumPy - GeeksforGeeks
Explanation: arr[[3, 1, 2]] creates a new array by picking elements from indices 3, 1 and 2 of the original array, resulting in [4, 8, 6]. Basic slicing and indexing are used to access specific elements or a range of elements from a NumPy array.
Published   November 4, 2025
🌐
datagy
datagy.io › home › numpy › indexing and slicing numpy arrays: a complete guide
Indexing and Slicing NumPy Arrays: A Complete Guide • datagy
December 30, 2022 - This comprehensive guide will teach you all the different ways to index and slice NumPy arrays. NumPy is an essential library for any data analyst or data scientist using Python. Effectively indexing and slicing NumPy arrays can make you a stronger programmer. By the end of this tutorial, you’ll have learned: ... Much like working with Python lists, NumPy arrays are based on a 0 index.
🌐
NumPy
numpy.org › doc › 1.21 › reference › arrays.indexing.html
Indexing — NumPy v1.21 Manual
Assume n is the number of elements in the dimension being sliced. Then, if i is not given it defaults to 0 for k > 0 and n - 1 for k < 0 . If j is not given it defaults to n for k > 0 and -n-1 for k < 0 . If k is not given it defaults to 1. Note that :: is the same as : and means select all indices along this axis. ... If the number of objects in the selection tuple is less than N, then : is assumed for any subsequent dimensions. ... >>> x = np.array([[[1],[2],[3]], [[4],[5],[6]]]) >>> x.shape (2, 3, 1) >>> x[1:2] array([[[4], [5], [6]]])
🌐
Mdjubayerhossain
mdjubayerhossain.com › numpy › notebooks › 04_ArraySlicingandSubsetting.html
Array Indexing and Slicing — Introduction to NumPy
One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences. The (start:stop:step) notation for slicing is used. ... Second, A2[0] select the first element of first row.
🌐
Earth Data Science
earthdatascience.org › home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - # Select the last element of the array avg_monthly_precip[-1] ... You can slice a range of elements from one-dimensional numpy arrays such as the third, fourth and fifth elements, by specifying an index range: [starting_value, ending_value].
🌐
StrataScratch
stratascratch.com › blog › numpy-array-slicing-in-python
NumPy Array Slicing in Python - StrataScratch
March 1, 2024 - In the given example, we first create a one-dimensional NumPy array with integers from 0 to 9 using np.arange(10). Then, we slice this array from index 1 to index 5 (inclusive of 1 and exclusive of 6), resulting in a new array sliced_arr that contains the elements [1 2 3 4 5]. This shows how slicing allows us to easily select a subset of an array's elements based on their indices...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.s_.html
numpy.s_ — NumPy v2.4 Manual
>>> import numpy as np >>> np.s_[2::2] slice(2, None, 2) >>> np.index_exp[2::2] (slice(2, None, 2),) >>> np.array([0, 1, 2, 3, 4])[np.s_[2::2]] array([2, 4])
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.s_.html
numpy.s_ — NumPy v2.1 Manual
>>> import numpy as np >>> np.s_[2::2] slice(2, None, 2) >>> np.index_exp[2::2] (slice(2, None, 2),) >>> np.array([0, 1, 2, 3, 4])[np.s_[2::2]] array([2, 4])
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.take.html
numpy.take — NumPy v2.2 Manual
‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers. ... The returned array has the same type as a. ... By eliminating the inner loop in the description above, and using s_ to build simple slice ...