I assume your array looks like:

       |(HUE)(VALUE)(CLASS)
row/col|   0     1     2
-------+-----------------
0      |   0     1     2
1      |   3     4     5
2      |   6     7     8
.      |   .     .     .
.      |   .     .     .
3599999|   .     .     .

And here is the sample code. For simplicity I changed the size 3600000 to 5.

a = np.array(xrange(5 * 3))
a.shape = (5, 3)

Now array a look like this:

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])

If you want row with HUE=9, do like this:

a[np.where(a[:,0] == 9)]
#array([[ 9, 10, 11]])

If you want row with VALUE=4, do like this:

a[np.where(a[:,1] == 4)]
#array([[3, 4, 5]])

If you want row with HUE=0 and VALUE=1, do like this:

a[np.where((a[:,0] == 0) * (a[:,1] == 1))]
#array([[0, 1, 2]])
Answer from Kei Minagawa on Stack Overflow
Top answer
1 of 2
2

You can use np.isin() to check which elements in the first column are in the desired values list. You can then use the output as a mask.

import numpy as np
my_array = np.array([[1,55,4],
                     [2,2,3],
                     [3,90,2],
                     [4,65,1]])
desired_values = np.array([2,3,4])
mask = np.isin(element = my_array[:,0],test_elements = desired_values)
desired_array = my_array[mask]
print(desired_array)

output

array([[ 2,  2,  3],
       [ 3, 90,  2],
       [ 4, 65,  1]])

Edit: numpy.isin vs for-loop

@Furas suggested a for-loop solution. That approach works and is perhaps more intuitive, at least in that one does not have to research the many esoteric functions of Numpy.

My first reaction is to emphasize that Numpy operations tend to be faster than for-loops. However, the details are a little more nuanced. For-loops seem to be slightly faster for small arrays, but their time costs increase significantly faster as the size of the array increases.

Comparisons for relatively small test arrays

The first graph compares the computation times for np.isin() to those from the for-loop. The number of rows in my_array are presented along the x-axis. Their values are 2, 4, 6, ... 32.

The above graph shows that the computation time for the for-loop seems to increase linearly with the growth of the tested array. The for-loop is faster until the number of rows is approximately 10.

Comparisons for larger test arrays

The second graph shows a comparison similar to the first graph, but examines computation times when the number of rows in my_array is 2, 4, 8, 16, ... 1024.

The above graph shows that np.isin() is significantly faster and more appropriate for larger problems.

Code for reproduction

The data to recreate the above graphs may be generated with the following code.

import numpy as np

count_list = [2**x for x in range(2,11)]
isin_time_means = []
loop_time_means = []

for count in count_list:
  my_array = np.random.randint(low=-10,high=10,size=(count,5))
  desired_values = np.random.randint(low=-10,high = 10,size=(10,))
  a = %timeit -o np.isin(my_array[:,0],desired_values)
  b = %timeit -o [x in desired_values for x in my_array[:,0]]
  isin_time_means.append(np.mean(a.timings))
  loop_time_means.append(np.mean(b.timings))
2 of 2
1

You can always use for-loop to check every value separatelly

mask = [x in desiredValues for x in myArray[:,0]]

desired_array = myArray[mask]

Full code:

import numpy as np

myArray = np.array([[1,55,4],
                     [2,2,3],
                     [3,90,2],
                     [4,65,1]])

desiredValues = [2,3,4]

mask = [x in desiredValues for x in myArray[:,0]]

desired_array = myArray[mask]

print(desired_array)
Discussions

python - Selecting specific rows and columns from NumPy array - Stack Overflow
I've been going crazy trying to figure out what stupid thing I'm doing wrong here. I'm using NumPy, and I have specific row indices and specific column indices that I want to select from. Here's the More on stackoverflow.com
๐ŸŒ stackoverflow.com
Select certain rows (condition met), but only some columns in Python/Numpy - Stack Overflow
I have an numpy array with 4 columns and want to select columns 1, 3 and 4, where the value of the second column meets a certain condition (i.e. a fixed value). I tried to first select only the row... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 28, 2014
python - Numpy select rows based on condition - Stack Overflow
I want to remove rows from a two dimensional numpy array using a condition on the values of the first row. I am able to do this with regular python using two loops, but I would like to do it more More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Selecting rows from a NumPy ndarray - Stack Overflow
I want to select only certain rows from a NumPy array based on the value in the second column. For example, this test array has integers from 1 to 10 in the second column. >>> test = numpy. More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 20, 2014
๐ŸŒ
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 ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.select.html
numpy.select โ€” NumPy v2.5 Manual
Beginning with an array of integers from 0 to 5 (inclusive), elements less than 3 are negated, elements greater than 3 are squared, and elements not meeting either of these conditions (exactly 3) are replaced with a default value of 42. >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [-x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, -1, -2, 42, 16, 25])
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]])
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: Extract or delete elements, rows, and columns that satisfy the conditions | note.nkmk.me
May 31, 2019 - Rows and columns are extracted by giving each result to [rows, :] or [:, columns]. For [rows, :], the trailing , : can be omitted.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-access-a-numpy-array-by-column
How to access a NumPy array by column - GeeksforGeeks
April 23, 2023 - For column : numpy_Array_name[ : ,column] For row : numpy_Array_name[ row, : ]
Find elsewhere
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ pandas_numpy โ€บ pandas_numpy-exercise-8.php
Filter Pandas DataFrame rows by NumPy array values in column
Extract rows from a Pandas DataFrame where a specific column's values are in a given NumPy array. ... import pandas as pd import numpy as np # Create a sample DataFrame data = {'Name': ['Teodosija', 'Sutton', 'Taneli', 'David', 'Emily'], 'Age': [25, 30, 22, 35, 28], 'Salary': [50000, 60000, 45000, 70000, 55000]} df = pd.DataFrame(data) # Define a NumPy array with values to filter by selected_age_values = np.array([25, 35]) # Extract rows where 'Age' column values are in the NumPy array selected_rows = df[df['Age'].isin(selected_age_values)] # Display the selected rows print(selected_rows)
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ conditional indexing: how to conditionally select elements in a numpy array?
Conditional Indexing: How to Conditionally Select Elements in a NumPy Array? - Be on the Right Side of Change
April 10, 2021 - Normal slicing such as a[i:j] would carve out a sequence between i and j. But selective indexing (also: conditional indexing) allows you to carve out an arbitrary combination of elements from the NumPy array by defining a Boolean array with the same shape. If the Boolean value at the index (i,j) is True, the element will be selected, otherwise not.
๐ŸŒ
Earth Data Science
earthdatascience.org โ€บ home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - For example, you can use [:, 0] to select the entire first column of precip_2002_2013, which are all of the values for January (in this case, for 2002 and 2013). ... Or conversely, you can use [0, :] to select the entire first row of precip_2002_2013, which are all of the monthly values for 2002. ... This means that you can create a new numpy array of the average monthly precipitation data in 2002 by slicing the first row of values from precip_2002_2013.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: Get and set values in an array using various indexing | note.nkmk.me
February 7, 2024 - Using a slice i:i+1 selects a single row or column, preserving the array's dimensions, unlike selection with an integer (int), which reduces the dimensions. NumPy: Get the number of dimensions, shape, and size of ndarray
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ selecting-specific-rows-and-columns-from-numpy-array
How to Select Specific Rows and Columns from a NumPy Array: Step-by-Step Guide with Examples โ€” pythontutorials.net
Basic indexing allows you to select individual rows or columns using their integer indices. Remember: NumPy uses 0-based indexing (the first element is index 0).
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.select.html
numpy.select โ€” NumPy v2.1 Manual
Beginning with an array of integers from 0 to 5 (inclusive), elements less than 3 are negated, elements greater than 3 are squared, and elements not meeting either of these conditions (exactly 3) are replaced with a default value of 42. >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, 1, 2, 42, 16, 25])
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ pandas-tips-select-rows-by-column-value
How to select rows by column value in Pandas | Saturn Cloud Blog
September 10, 2023 - import numpy as np #select by scalar value data[data['Age'].values == 2] #select by iterable value data[np.in1d(data['Age'].values, [2, 5])] To wrap up, there are a variety of ways to select DataFrame rows by column value. Boolean indexing (with or without loc) offers a quick and intuitive way to index DataFrames, especially for smaller datasets.
๐ŸŒ
w3tutorials
w3tutorials.net โ€บ blog โ€บ select-certain-rows-condition-met-but-only-some-columns-in-python-numpy
How to Select Specific Rows (with Condition) and Columns in Python NumPy โ€” w3tutorials.net
NumPy uses 0-based indexing (the first element is at index 0). Basic indexing lets you select rows/columns by their position using array[row_index, column_index].
๐ŸŒ
Medium
medium.com โ€บ @iambeniwal12 โ€บ how-to-select-rows-from-a-dataframe-based-on-column-values-in-pandas-83b091bade91
How to Select Rows from a DataFrame Based on Column Values in Pandas | by Narender Beniwal | Medium
November 1, 2024 - To select rows based on a specific column value, you can use the df.loc[] method combined with a condition. df.loc[df['column_name'] == some_value] import pandas as pd import numpy as np ยท