Extracting specific columns in numpy array
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Extracting specific columns from a NumPy array uses array indexing. For a 2D array, arr[:, col_index] extracts a single column, and arr[:, [col1, col2]] extracts multiple columns. The : selects all rows, and the second index selects columns. NumPy also supports boolean indexing and fancy indexing for more complex column selection patterns.
Single Column
Note: data[:, 0] returns a 1D array of shape (3,). To keep it as a column vector (2D), use data[:, [0]] or data[:, 0:1].
Multiple Columns
Column Range (Slicing)
Boolean Column Selection
Named Columns with Structured Arrays
Extracting Columns into Separate Variables
Views vs Copies
Performance with Large Arrays
Common Pitfalls
- Confusing
data[:, 0](1D) withdata[:, [0]](2D): Single index[:, 0]returns a 1D array of shape(n,). List index[:, [0]]returns a 2D column of shape(n, 1). This matters for operations that require 2D input like matrix multiplication. - Modifying a view and changing the original: Slice-based column extraction (
data[:, 1:3]) returns a view. Modifying the view modifies the original array. Use.copy()if you need an independent copy. - Fancy indexing returning a copy: Unlike slicing,
data[:, [0, 2]]returns a copy, not a view. Modifying the result does not affect the original. This inconsistency catches many people. - Using negative indices incorrectly:
data[:, -1]gets the last column, butdata[:, -1:]gets a 2D array with one column. The results have different shapes (1D vs 2D), which affects downstream operations. - Index out of bounds:
data[:, 5]on a 4-column array raisesIndexError. Checkdata.shape[1]before accessing columns by index, especially with dynamic data.
Summary
- Use
arr[:, col]for a single column andarr[:, [col1, col2]]for multiple columns - Use slicing
arr[:, start:end]for column ranges - Boolean arrays can select columns based on conditions
- Slicing creates views (shared memory); fancy indexing creates copies
- Use
.T(transpose) to unpack columns into separate variables - Check
arr.shape[1]for the number of columns before indexing
Related reading
- Extracting specific selected columns to new DataFrame as a copy
- f1_score metric in lightgbm
- Failed to convert a NumPy array to a Tensor Unsupported object type numpy.ndarray - Already have converted the data to numpy array
- Fast algorithm for repeated calculation of percentile?
- F List.map equivalent in C?
- Failed building wheel for dm-tree
- Extracting text from HTML file using Python
- Facing ValueError Target is multiclass but average'binary

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.