Transpose a Matrix with NumPy
Swap rows and columns with .T: the value at [i, j] moves to [j, i].
- matrix.T and np.transpose
- How the shape changes
- Transposing views, not copies
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.
matrix🗓️ 2 rows, 3 columnsmatrix.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 columnsTransposing a matrix swaps its rows and columns, so the element at row i, column j moves to row j, column i.
Key points
matrixhas shape (2, 3): two rows of three values.matrix.Thas shape (3, 2): the first row [1, 2, 3] becomes the first column.- Each value keeps its number; only its position changes.
.Treturns a view, so no data is copied andmatrixitself 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:
.Tdoes nothing to it; add a dimension first withreshape(-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
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.T)
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.
- Change the input shape and dtype.
- Try negative, floating-point, and missing values.
- Apply the operation to larger multidimensional arrays and measure performance.
Common mistakes
Using reshape to transpose
reshape keeps the order of values, so the result is not a transpose.
matrix.reshape(3, 2) # [[1 2] [3 4] [5 6]]
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.
- Matrix Multiplication Multiply two matrices, the operation behind every layer of a neural network. Easy
- Scaled Dot-Product Attention Implement the attention operation at the heart of Transformers and large language models. Hard
- K-Means Clustering Group points into k clusters by repeatedly assigning them to the nearest centroid and moving the centroids. Medium