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]])
Answer from Praveen on Stack Overflow
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]])
๐ŸŒ
ProjectPro
projectpro.io โ€บ recipes โ€บ select-elements-from-numpy-array-in-python
How to Select Columns in NumPy Array using np.select? -
February 22, 2024 - You can use array slicing with a specified column index. ... The expression arr[:, 1:3] selects all rows (indicated by :) and the second and third columns (columns with index 1 and 2).
๐ŸŒ
thisPointer
thispointer.com โ€บ home โ€บ python โ€บ select rows / columns by index in numpy array
Select Rows / Columns by Index in NumPy Array - thisPointer
November 12, 2023 - To select a column, pass the column index along with the rows information in the [] operator of NumPy Array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-access-a-numpy-array-by-column
How to access a NumPy array by column - GeeksforGeeks
June 26, 2025 - Slicing is the easiest and fastest way to access a specific column in a NumPy 2D array using arr[:, column_index], where : selects all rows and column_index picks the desired column.
๐ŸŒ
Earth Data Science
earthdatascience.org โ€บ home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - You can use shortcuts to easily select an entire row or column by simply specifying the index of the row or column (e.g.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.5 Manual
Use boolean indexing to select all rows adding up to an even number. At the same time columns 0 and 2 should be selected with an advanced integer index.
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ numpy_select_columns_by_index.php
Numpy select columns by index
For a 2D array (matrix), indexing follows the format [row_index, column_index]. array[0, 0] selects the element at the first row, first column. array[1, :] selects all elements from the second row. (The : means "all elements along this dimension") array[:, 2] selects all elements from the third ...
Find elsewhere
๐ŸŒ
Kanoki
kanoki.org โ€บ numpy-get-ith-column-and-specific-column-row-data-from-array
Numpy get ith column and specific column and row data from an array | kanoki
October 6, 2022 - Ellipsis expands to the number of:objects needed for the selection tuple to index all dimensions. ... We want the 2nd and 3rd column of the array a. ... We want the last column of the array, we could use negative indices for indexing from the end of the array ... The output array is 1D of shape 5. We could also change the shape of the output array by using newaxis.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-extract-specific-columns-from-a-NumPy-array-in-Python
How to extract specific columns from a NumPy array in Python - Quora
Answer (1 of 3): The simplest way is probably to use the standard indexing system with slicing. An element in a numpy array can be specified by using its indices normally such as arr[row, col] However, NumPy also allows for slicing, e.g. arr[1:4, 2], which returns the elements in column 2 (the ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-access-a-numpy-array-by-column
How to access a NumPy array by column?
Use the colon ":" operator to select all rows and specify the column index ? import numpy as np # Create a sample NumPy array array = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) # Access the third column (index 2) column = array[:, 2] print("Third column:") print(column) ... Fancy indexing allows you to access multiple columns simultaneously by passing an array of column indices ?
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to get specific column from numpy array (with examples)
How to Get Specific Column from NumPy Array (With Examples)
September 16, 2021 - If youโ€™d like to get a column from a NumPy array and retrieve it as a column vector, you can use the following syntax: #get column in index position 2 (as a column vector) data[:, [2]] array([[ 3], [ 7], [11]])
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ numpy โ€บ numpy get column
How to Get Column of NumPy Array | Delft Stack
March 14, 2025 - This method allows you to specify the row and column indices you want to retrieve. In a 2D array, you can access a specific column by using the syntax array[:, column_index]. Hereโ€™s how it works:
๐ŸŒ
Medium
medium.com โ€บ @shivamkaus โ€บ select-columns-by-index-range-pythonpandas-com-51c7afb8e578
Select columns by index range โ€” PythonPandas.com | by Shivam Kau | Medium
July 2, 2025 - Although .loc uses labels (not indexes), you can still select by index first and then pass the labels. # Get column labels using index range column_labels = df.columns[1:4] # Use loc with the column labels subset = df.loc[:, column_labels] print(subset) This is similar to method 2 but makes use of loc. MethodApproachCode ExampleilocIndex-based slicingdf.iloc[:, 1:4]df.columnsGet column names by indexdf[df.columns[1:4]]NumPyUse array of indexesdf.iloc[:, np.arange(1,4)]Specific indexesNon-sequentialdf.iloc[:, [0, 2, 4]]loc + df.columnsLabel-baseddf.loc[:, df.columns[1:4]]
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.select.html
numpy.select โ€” NumPy v2.1 Manual
>>> 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])
๐ŸŒ
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
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.take.html
numpy.take โ€” NumPy v2.2 Manual
Explained without fancy indexing, this is equivalent to the following use of ndindex, which sets each of ii, jj, and kk to a tuple of indices: Ni, Nk = a.shape[:axis], a.shape[axis+1:] Nj = indices.shape for ii in ndindex(Ni): for jj in ndindex(Nj): for kk in ndindex(Nk): out[ii + jj + kk] = a[ii + (indices[jj],) + kk] ... The source array. ... The indices of the values to extract. Also allow scalars for indices. ... The axis over which to select values. By default, the flattened input array is used.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.6.dev0 Manual
Use boolean indexing to select all rows adding up to an even number. At the same time columns 0 and 2 should be selected with an advanced integer index.