How do I select certain columns of a 2D tensor in TensorFlow?
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
Selecting columns from a 2D TensorFlow tensor is a common preprocessing task in models, feature pipelines, and tensor debugging. The right operation depends on whether the columns are contiguous, whether the selection is dynamic, and whether you want to preserve tensor shape in a specific way. In most cases, simple slicing or tf.gather is the correct answer.
Use Slicing for Contiguous Columns
If the columns you want are next to each other, ordinary tensor slicing is the simplest option.
This selects all rows and columns 1 through 2. The stop index is exclusive, just like normal Python slicing.
Slicing is ideal when the range is contiguous and known directly.
Use tf.gather for Arbitrary Column Indices
If you want non-contiguous columns such as 0 and 2, use tf.gather along axis 1.
This is the most common general answer for “select these specific columns.”
Why axis=1 Matters
In a 2D tensor:
- axis 0 means rows
- axis 1 means columns
So:
selects columns, while:
would select rows instead.
Many TensorFlow indexing bugs come from mixing up the axis.
Single Column Selection
If you want one column, you can use slicing or tf.gather.
Slicing:
This returns a rank-1 tensor with shape (rows,).
If you want to preserve a 2D shape such as (rows, 1), use:
That difference is important in model code where a downstream layer expects a specific rank.
Boolean Masking for Column Selection
If the selection rule is expressed as a boolean mask, tf.boolean_mask can help.
This is less common than tf.gather, but useful when column selection is naturally described as a mask.
Dynamic Selection
If the column indices are computed at runtime, tf.gather is still the usual solution.
This works in eager execution and inside tf.function, which makes it a good default for model pipelines.
TensorFlow Shape Awareness
After selecting columns, check the resulting shape if the next operation depends on a specific layout.
It is easy to accidentally drop a dimension with x[:, 2] when a downstream layer expects x[:, 2:3].
That is not a TensorFlow bug. It is the result of the indexing choice.
Practical Rule of Thumb
Use:
- slicing for contiguous known ranges
- '
tf.gatherfor specific column index lists' - '
tf.boolean_maskwhen selection is naturally boolean-driven'
That covers most real 2D column-selection scenarios cleanly.
Common Pitfalls
The biggest mistake is mixing up row selection and column selection by using the wrong axis. Another is expecting x[:, 2] to keep a two-dimensional shape when it returns rank 1. Developers also sometimes use tf.gather for simple contiguous ranges where slicing is clearer. Finally, shape mismatches after column selection often show up later in model code, so it is worth checking the output shape immediately.
Summary
- Use normal slicing for contiguous column ranges.
- Use
tf.gather(..., axis=1)for arbitrary column index selection. - Use
x[:, 2:3]instead ofx[:, 2]if you need to preserve a 2D column shape. - '
tf.boolean_maskis useful when the selection is driven by a boolean mask.' - Always verify the resulting shape before feeding the tensor downstream.
Related reading
- How do I set TensorFlow \`RNN\` state when state_is_tupleTrue?
- How do I set TensorFlow \`RNN\` state when state_is_tupleTrue?
- How do I set up TensorFlow in the Google cloud?
- How do I set up TensorFlow in the Google cloud?
- How do I shape my input data for use with Conv1D in keras?
- How do I solve overfitting in random forest of Python sklearn?
- How do I select rows from a DataFrame based on column values?
- How do I send something to connected websocket clients from another thread?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.