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.5 Manual
Integer array indexing allows selection of arbitrary items in the array based on their N-dimensional index. Each integer array represents a number of indices into that dimension.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.take.html
numpy.take — NumPy v2.2 Manual
The indices of the values to extract. Also allow scalars for indices. ... The axis over which to select values.
Top answer
1 of 4
151

As Toan suggests, a simple hack would be to just select the rows first, and then select the columns over that.

>>> a[[0,1,3], :]            # Returns the rows you want
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [12, 13, 14, 15]])
>>> a[[0,1,3], :][:, [0,2]]  # Selects the columns you want as well
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

[Edit] The built-in method: np.ix_

I recently discovered that numpy gives you an in-built one-liner to doing exactly what @Jaime suggested, but without having to use broadcasting syntax (which suffers from lack of readability). From the docs:

Using ix_ one can quickly construct index arrays that will index the cross product. a[np.ix_([1,3],[2,5])] returns the array [[a[1,2] a[1,5]], [a[3,2] a[3,5]]].

So you use it like this:

>>> a = np.arange(20).reshape((5,4))
>>> a[np.ix_([0,1,3], [0,2])]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

And the way it works is that it takes care of aligning arrays the way Jaime suggested, so that broadcasting happens properly:

>>> np.ix_([0,1,3], [0,2])
(array([[0],
        [1],
        [3]]), array([[0, 2]]))

Also, as MikeC says in a comment, np.ix_ has the advantage of returning a view, which my first (pre-edit) answer did not. This means you can now assign to the indexed array:

>>> a[np.ix_([0,1,3], [0,2])] = -1
>>> a    
array([[-1,  1, -1,  3],
       [-1,  5, -1,  7],
       [ 8,  9, 10, 11],
       [-1, 13, -1, 15],
       [16, 17, 18, 19]])
2 of 4
102

Fancy indexing requires you to provide all indices for each dimension. You are providing 3 indices for the first one, and only 2 for the second one, hence the error. You want to do something like this:

>>> a[[[0, 0], [1, 1], [3, 3]], [[0,2], [0,2], [0, 2]]]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

That is of course a pain to write, so you can let broadcasting help you:

>>> a[[[0], [1], [3]], [0, 2]]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

This is much simpler to do if you index with arrays, not lists:

>>> row_idx = np.array([0, 1, 3])
>>> col_idx = np.array([0, 2])
>>> a[row_idx[:, None], col_idx]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.take.html
numpy.take — NumPy v2.5 Manual
The indices of the values to extract. Also allow scalars for indices. ... The axis over which to select values.
🌐
NumPy
numpy.org › doc › 1.21 › reference › arrays.indexing.html
Indexing — NumPy v1.21 Manual
June 22, 2021 - ... Assume n is the number of elements ... 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....
🌐
thisPointer
thispointer.com › home › numpy › numpy – select elements by condition
NumPy - Select Elements By Condition - thisPointer
April 29, 2023 - Let’s select elements from it. Let’s apply < operator on above created numpy array i.e. # Comparison Operator will be applied to all elements in array boolArr = arr < 10
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › arrays.indexing.html
Basic Slicing and Indexing - Numpy and Scipy Documentation
June 10, 2017 - ... Assume n is the number of elements ... 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....
🌐
ProjectPro
projectpro.io › recipes › select-elements-from-numpy-array-in-python
How to Select Columns in NumPy Array using np.select? -
February 22, 2024 - The expression arr[:, 1:3] selects all rows (indicated by :) and the second and third columns (columns with index 1 and 2). Adjust the column indices in the slice as needed. You can use array slicing with a step size to select every nth element ...
Find elsewhere
🌐
NumPy
numpy.org › doc › 1.16 › reference › arrays.indexing.html
Indexing — NumPy v1.16 Manual
February 18, 2020 - ... Assume n is the number of elements ... 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....
🌐
IncludeHelp
includehelp.com › python › select-all-elements-in-a-numpy-array-except-for-a-sequence-of-indices.aspx
Python - Select all elements in a NumPy array except for a sequence of indices?
December 28, 2023 - We will pass the list of indices and input array in this function and it will remove all the elements at the position of the index's elements. ... # Import numpy import numpy as np # Creating a numpy array arr = np.array([0,10,20,30,40,50,60]) # Display original array print("Original array:\n",arr,"\n") # List of indices ind = [1,3,5] # Selecting all elements except list of indices res = np.delete(arr, ind) # Display result print("Result:\n",res)
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-92.php
NumPy: Select indices satisfying multiple conditions in a NumPy array - w3resource
August 29, 2025 - Write a NumPy program to select indices of an array that satisfy multiple conditions using np.where and logical operators.
🌐
NumPy
numpy.org › devdocs › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.6.dev0 Manual
Integer array indexing allows selection of arbitrary items in the array based on their N-dimensional index. Each integer array represents a number of indices into that dimension.
🌐
Quora
quora.com › How-do-I-select-elements-from-a-NumPy-array-in-Python
How to select elements from a NumPy array in Python - Quora
Answer (1 of 2): Selecting an element is very easy if you know the index of the element. In this we will be selecting element from vector matrix and tensor. So this is the recipe on how we can Select Elements from Numpy Array. Step 1 - Import the library [code ] import numpy as np [/code] We ...
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_indexing.asp
NumPy Array Indexing
The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
Top answer
1 of 3
4

There are some higher-level functions, but let's see how to do it using just the simplest stuff in the library, because you're going to need those simple functions every day.

>>> matches = (I == 2)
>>> matches
array([False, False,  True, False, False,  True, False, False, False,
       False, False,  True, False,  True, False], dtype=bool)    
>>> indices = np.nonzero(matches)
>>> indices
(array([ 2,  5, 11, 13]),)
>>> xvals = X[indices]
>>> xvals
array([[ 3.6 ,  2.01],
       [ 3.9 ,  7.02],
       [ 4.5 ,  7.55],
       [ 4.7 ,  0.33]])

The last step may look confusing. See Indexing in the tutorial for further information.

Once you understand how the == operator and nonzero work, look through the other functions in the same section as nonzero and you should find two shorter ways to do this.

2 of 3
2

If you would like to try pandas, it's really powerful in groupby data. Here's how you can achieve what you need:

In [34]: import numpy as np

In [35]: import pandas as pd

#I defined you X, I already
In [36]: X
Out[36]: 
array([[ 3.4 ,  9.13],
       [ 3.5 ,  3.43],
       [ 3.6 ,  2.01],
       [ 3.7 ,  6.11],
       [ 3.8 ,  4.95],
       [ 3.9 ,  7.02],
       [ 4.  ,  4.41],
       [ 4.1 ,  0.23],
       [ 4.2 ,  0.99],
       [ 4.3 ,  1.02],
       [ 4.4 ,  5.61],
       [ 4.5 ,  7.55],
       [ 4.6 ,  8.1 ],
       [ 4.7 ,  0.33],
       [ 4.8 ,  0.8 ]])

In [37]: I
Out[37]: array([0, 1, 2, 0, 1, 2, 3, 0, 1, 0, 1, 2, 0, 2, 1], dtype=int64)

In [38]: dataframe=pd.DataFrame (data=X, index=I, columns=['X1','X2'])

In [39]: dataframe.index.name='I' #This is not necessary
In [40]: print dataframe
    X1    X2
I           
0  3.4  9.13
1  3.5  3.43
2  3.6  2.01
0  3.7  6.11
1  3.8  4.95
2  3.9  7.02
3  4.0  4.41
0  4.1  0.23
1  4.2  0.99
0  4.3  1.02
1  4.4  5.61
2  4.5  7.55
0  4.6  8.10
2  4.7  0.33
1  4.8  0.80

This defines a dataframe with I as index and X as data. Now if you need rows with I=2, you can do

In [42]: print dataframe.ix[2]
    X1    X2
I           
2  3.6  2.01
2  3.9  7.02
2  4.5  7.55
2  4.7  0.33

If you want to list all groups:

In [43]: for i, grouped_data in dataframe.groupby(level='I'): #without level=, you can group by a regular column like X1
   ....:     print i
   ....:     print grouped_data
   ....:     
0
    X1    X2
I           
0  3.4  9.13
0  3.7  6.11
0  4.1  0.23
0  4.3  1.02
0  4.6  8.10
1
    X1    X2
I           
1  3.5  3.43
1  3.8  4.95
1  4.2  0.99
1  4.4  5.61
1  4.8  0.80
2
    X1    X2
I           
2  3.6  2.01
2  3.9  7.02
2  4.5  7.55
2  4.7  0.33
3
   X1    X2
I          
3   4  4.41

If you just want to see statistics of each group, you can do

In [47]: print dataframe.groupby(level='I').sum() #try other funcs like mean, var, .
     X1     X2
I             
0  20.1  24.59
1  20.7  15.78
2  16.7  16.91
3   4.0   4.41