HOWTO · NumPy
How to Get a Column From a NumPy Array
Select one or more columns from a 2-D NumPy array, preserve the required shape, filter rows, and choose between a view and a copy.
On this page
For a two-dimensional NumPy array, use array[:, column_index] to select one column. The colon selects every row, and the integer selects a zero-based column. For example, data[:, 1] returns the second column as a one-dimensional array. Use data[:, 1:2] instead when later code requires a two-dimensional (rows, 1) result.
Select One Column and Control Its Shape
NumPy indexes a 2-D array as array[row_selection, column_selection]. Leaving out the comma changes the operation: data[:][1] first creates a full slice and then selects row 1, so it does not select column 1.
The following example covers the common column-selection cases in one reproducible program:
import numpy as np
data = np.array([[10, 11, 12], [20, 21, 22], [30, 31, 32]])
second = data[:, 1]
second_2d = data[:, 1:2]
adjacent = data[:, :2]
reordered = data[:, [2, 0]]
taken = np.take(data, [0, 2], axis=1)
filtered = data[data[:, 0] >= 20, 2]
print("second:", second, second.shape)
print("second_2d:\n", second_2d, second_2d.shape)
print("adjacent:\n", adjacent)
print("reordered:\n", reordered)
print("taken:\n", taken)
print("filtered:", filtered)
view = data[:, 1]
copy = data[:, 1].copy()
view[0] = 99
print("data after view edit:\n", data)
print("copy:", copy)
Output:
second: [11 21 31] (3,)
second_2d:
[[11]
[21]
[31]] (3, 1)
adjacent:
[[10 11]
[20 21]
[30 31]]
reordered:
[[12 10]
[22 20]
[32 30]]
taken:
[[10 12]
[20 22]
[30 32]]
filtered: [22 32]
data after view edit:
[[10 99 12]
[20 21 22]
[30 31 32]]
copy: [11 21 31]
An integer column index removes that axis, which explains the (3,) shape of second. A one-element slice retains the axis, producing the (3, 1) shape of second_2d. This distinction matters when an API expects a matrix-like input rather than a flat vector.
Select Multiple Columns
Use a slice such as data[:, :2] for adjacent columns. Slicing expresses a continuous range and usually produces a view. For non-adjacent columns or a custom order, provide an index list: data[:, [2, 0]]. This is advanced indexing and produces a copy rather than a view.
An index list always keeps a column axis. Thus, data[:, [1]] has shape (3, 1), while data[:, 1] has shape (3,). A list can also repeat a column or change its position, so validate dynamically supplied indices when duplicates or ordering would be surprising. Use a slice when the columns form a simple range and an index list when the selection itself is a sequence.
np.take is an axis-explicit alternative. np.take(data, [0, 2], axis=1) selects columns 0 and 2; axis=1 is essential because columns occupy the second axis. It is particularly useful when the axis or indices are stored in variables.
Filter Rows Before Reading a Column
A Boolean condition normally identifies rows, not columns. Build the row mask from one column, then place the requested output column after the comma. In the example, data[:, 0] >= 20 keeps the final two rows, and data[data[:, 0] >= 20, 2] returns [22 32] from column 2. The mask length must match the number of rows.
Decide Whether You Need a View or a Copy
According to NumPy’s indexing rules, basic slicing returns a view when possible, whereas advanced indexing returns a copy. Consequently, assigning through view = data[:, 1] can change data, as the 99 in the output demonstrates. Call .copy() when the extracted column must be independent. The copied values remain [11 21 31] after the view changes the source.
These views also keep their base array’s memory reachable. Copy a small column if retaining a large source array would waste memory after the rest of that source is no longer needed.
Check Dimensions and Column Bounds
These expressions assume a 2-D array. A 1-D array has no column axis, so array[:, 0] raises an indexing error. Check array.ndim == 2 when input shape is uncertain. For a 2-D array, valid nonnegative column indices run from 0 through array.shape[1] - 1; an index outside that range raises IndexError. Negative indices are also valid within bounds, so array[:, -1] selects the last column.