Run NumPy Online

Transpose a Matrix with NumPy

Swap rows and columns with .T: the value at [i, j] moves to [j, i].

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • matrix.T and np.transpose
  • How the shape changes
  • Transposing views, not copies
[[1, 2, 3], [4, 5, 6]].T
1
4
2
5
3
6
The second row 4 5 6 is now the second column.

Rows become columns

The problem

Data is often stored the wrong way around: one row per month when you need one row per product, or a matrix whose shape does not fit a multiplication. Transposing swaps rows and columns in one step.

Think of it like this

Transposing is like rewriting a timetable with its headings swapped. What was written across the top now goes down the side: the first row becomes the first column, and the second row the second column. Every entry keeps its value; only its place changes.

From rewriting a timetable to code
matrix🗓️ 2 rows, 3 columns
matrix.T🔄 Rewrite it with the rows as columns
[i, j] → [j, i]↘️ Row i, column j moves to row j, column i
(3, 2)📐 Now 3 rows, 2 columns
Rows become columns and columns become rows.
In one sentence

Transposing a matrix swaps its rows and columns, so the element at row i, column j moves to row j, column i.

Key points

  1. matrix has shape (2, 3): two rows of three values.
  2. matrix.T has shape (3, 2): the first row [1, 2, 3] becomes the first column.
  3. Each value keeps its number; only its position changes.
  4. .T returns a view, so no data is copied and matrix itself is unchanged.

When should you use it?

  • Making shapes fit for matrix multiplication.
  • Switching between one-row-per-item and one-column-per-item layouts.
  • Linear algebra formulas that use transposed matrices.

When not to use it

  • Changing the number of rows and columns without swapping them: use reshape.
  • A one-dimensional array: .T does nothing to it; add a dimension first with reshape(-1, 1).

See how it works

Press transpose to swap rows and columns. Point at a cell to see its new position and memory offset.

Example code

Run code →
main.py
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.T)
Get a Run button for this example

Result of running the code

[[1 4]
 [2 5]
 [3 6]]

Code explanation

matrix has two rows and three columns. The .T attribute swaps the axes, so each original row becomes a column: 1, 2 and 3 now run down the first column. The result has shape (3, 2), and because .T returns a view of the same data, matrix itself is not changed.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input shape and dtype.
  2. Try negative, floating-point, and missing values.
  3. Apply the operation to larger multidimensional arrays and measure performance.
Run in the Python compiler →

Common mistakes

Using reshape to transpose

reshape keeps the order of values, so the result is not a transpose.

Problem
matrix.reshape(3, 2)  # [[1 2] [3 4] [5 6]]
Fix
matrix.T  # [[1 4] [2 5] [3 6]]

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges