TensorFlow
2D tensor
column selection
machine learning
Python

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.

Practice ML system design

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.

python
1import tensorflow as tf
2
3x = tf.constant([
4    [10, 11, 12, 13],
5    [20, 21, 22, 23],
6    [30, 31, 32, 33]
7])
8
9selected = x[:, 1:3]
10print(selected)

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.

python
1import tensorflow as tf
2
3x = tf.constant([
4    [10, 11, 12, 13],
5    [20, 21, 22, 23],
6    [30, 31, 32, 33]
7])
8
9selected = tf.gather(x, indices=[0, 2], axis=1)
10print(selected)

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:

python
tf.gather(x, indices=[0, 2], axis=1)

selects columns, while:

python
tf.gather(x, indices=[0, 2], axis=0)

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:

python
col = x[:, 2]
print(col)

This returns a rank-1 tensor with shape (rows,).

If you want to preserve a 2D shape such as (rows, 1), use:

python
col2d = x[:, 2:3]
print(col2d)

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.

python
1import tensorflow as tf
2
3x = tf.constant([
4    [10, 11, 12, 13],
5    [20, 21, 22, 23],
6    [30, 31, 32, 33]
7])
8
9mask = tf.constant([True, False, True, False])
10selected = tf.boolean_mask(x, mask, axis=1)
11print(selected)

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.

python
1import tensorflow as tf
2
3x = tf.constant([
4    [10, 11, 12, 13],
5    [20, 21, 22, 23]
6])
7
8indices = tf.constant([1, 3])
9selected = tf.gather(x, indices=indices, axis=1)
10print(selected)

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.

python
print(x.shape)
print(selected.shape)

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.gather for specific column index lists'
  • 'tf.boolean_mask when 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 of x[:, 2] if you need to preserve a 2D column shape.
  • 'tf.boolean_mask is useful when the selection is driven by a boolean mask.'
  • Always verify the resulting shape before feeding the tensor downstream.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design