Written and maintained by CASRAI Editorial Board
Last updated
To transpose an array in NumPy, call np.transpose(a) or use the shorthand a.T: for a 2-D array this swaps rows and columns, so a (2, 3) array becomes (3, 2). Two points cause most confusion. A 1-D array is returned unchanged, because a transposed vector is the same vector. And for arrays with three or more axes, the default reverses all axes, not just the last two. This guide shows how to read the axes argument, how to undo a transpose, how to turn a 1-D array into a column, and when to use np.matrix_transpose instead.
What transposing is used for in research
Transposing flips the orientation of a table of numbers so that observations and variables swap places. Common research uses are matching the orientation a function expects (samples as rows and variables as columns, or the reverse), forming products such as XTX in regression and covariance work, and rearranging image or time-series stacks so the axis you want to loop over comes first. In every case the data are unchanged; only the order of the axes is.
Install or access
Transposing is built into NumPy, so there is nothing extra to install. The NumPy install page gives pip install numpy. For conda, create and activate an environment, then run conda install numpy. To keep project dependencies separate with a virtual environment, run python -m venv my-env, then source my-env/bin/activate on macOS and Linux or my-env\Scripts\activate on Windows, then pip install numpy. Check the install with:
import numpy as np
print(np.__version__)
Step-by-step: transpose an array
Transpose a matrix (numpy transpose matrix)
- Import NumPy and create a 2-D array:
a = np.array([[1, 2, 3], [4, 5, 6]]). Itsshapeis (2, 3). - Transpose it with the function,
np.transpose(a), or the attribute,a.T. The NumPy reference definesndarray.Tas the same asself.transpose(). - Check the result: the shape is now (3, 2), with rows
[1, 4],[2, 5]and[3, 6].
The signature is numpy.transpose(a, axes=None). With axes=None the default is range(a.ndim)[::-1], which reverses the order of the axes. For a 2-D array that is the ordinary matrix transpose.
Transpose a 3-D array with the axes argument
For three or more dimensions, axes lets you choose the new order. The NumPy reference says it must be a tuple or list containing a permutation of [0, 1, …, N-1], where N is the number of axes of a, and that negative indices can also be used. Read it as: output axis i is input axis axes[i].
x = np.arange(24).reshape(2, 3, 4) # shape (2, 3, 4)
np.transpose(x, (2, 0, 1)).shape # (4, 2, 3)
np.transpose(x, (1, 0, 2)).shape # (3, 2, 4)
x.T.shape # (4, 3, 2): all axes reversed
In the first line, output axis 0 is input axis 2 (length 4), output axis 1 is input axis 0 (length 2) and output axis 2 is input axis 1 (length 3), giving (4, 2, 3). The NumPy reference also gives np.arange(60).reshape(3, 4, 5) with axes=(-1, 0, -2) producing shape (5, 3, 4).
Undo a transpose
The NumPy notes say to use transpose(a, argsort(axes)) to invert a transposition made with the axes keyword. Using the array above:
axes = (2, 0, 1)
y = np.transpose(x, axes) # shape (4, 2, 3)
np.argsort(axes) # array([1, 2, 0])
z = np.transpose(y, np.argsort(axes)) # shape (2, 3, 4), same as x
See the NumPy argsort guide for how argsort returns the indices that would sort an array, which is exactly what inverts a permutation.
Transpose a 1-D array
The NumPy reference states that for a 1-D array, transpose returns an unchanged view of the original array, as a transposed vector is simply the same vector. To convert a 1-D array into a 2-D column vector, an additional dimension must be added. The reference gives two equivalent ways:
v = np.array([1, 2, 3])
v.T.shape # (3,) unchanged
np.atleast_2d(v).T.shape # (3, 1)
v[:, np.newaxis].shape # (3, 1)
Transpose a stack of matrices with np.matrix_transpose
numpy.matrix_transpose(x, /) transposes a matrix or a stack of matrices: an input of shape (…, M, N) becomes (…, N, M), so only the last two axes swap. The reference describes it as Array API compatible. For s = np.arange(24).reshape(2, 3, 4), np.matrix_transpose(s).shape is (2, 4, 3), whereas s.T.shape is (4, 3, 2). Use it when each leading index is a separate matrix, such as a batch of covariance matrices.
Output and how to read it
Check shape first, since it records the new axis order. The NumPy reference says a view is returned whenever possible, so the result usually shares memory with the original array. Tested with NumPy 2.5.1, np.shares_memory(m, m.T) is True, and assigning to m.T[0, 0] changes m[0, 0]. If you need an independent copy, make one explicitly, for example m.T.copy(). A transposed view is not C-contiguous in memory, which matters only if a downstream routine requires contiguous input; np.ascontiguousarray(m.T) returns a contiguous copy.
Troubleshooting
The transpose did nothing
Your array is almost certainly 1-D, with shape like (3,). Add an axis as shown in the 1-D section above, then transpose. Print a.shape before and after to confirm.
ValueError: axes don’t match array
The axes tuple must have exactly one entry per dimension. With NumPy 2.5.1, passing a three-entry tuple to a 2-D array raises ValueError: axes don't match array.
ValueError: repeated axis in transpose
Each axis may appear once. (0, 0) on a 2-D array raises ValueError: repeated axis in transpose in NumPy 2.5.1. Pass a true permutation such as (1, 0).
Changing the transposed array changed my original
This follows from the view behaviour described above. Call .copy() on the transposed array when you need independent data.
Free alternatives and equivalents
| Tool | Syntax | What it does (per official docs) |
|---|---|---|
NumPy ndarray.T |
a.T |
View of the transposed array; same as self.transpose(). |
NumPy numpy.transpose |
np.transpose(a, axes=None) |
Reverses or permutes axes; returns a view whenever possible. |
NumPy numpy.matrix_transpose |
np.matrix_transpose(x) |
Swaps the last two axes of a matrix or stack of matrices, (…, M, N) to (…, N, M). |
NumPy numpy.swapaxes |
np.swapaxes(a, axis1, axis2) |
Interchanges two named axes; for ndarray input a view is returned (NumPy 1.10.0 and later). |
pandas DataFrame.T |
df.T |
Returns a DataFrame with rows and columns interchanged, reflecting the data across the main diagonal. |
For a labelled table of mixed column types, pandas DataFrame.T keeps the index and column labels; for numeric arrays, the NumPy forms above are the direct route. For related array basics, see the guides on creating NumPy arrays, np.zeros and numpy.linalg.norm.
FAQ
How do I transpose a matrix in NumPy?
Use a.T or np.transpose(a). For a 2-D array this swaps rows and columns, so shape (2, 3) becomes (3, 2).
What is the difference between np.transpose and .T?
For the default case there is none: the NumPy reference defines a.T as the same as a.transpose(). Use np.transpose(a, axes) or a.transpose(axes) when you want a specific axis order.
Why does transposing a 1-D array not change it?
A transposed vector is the same vector, so NumPy returns an unchanged view. Add a dimension with a[:, np.newaxis] or np.atleast_2d(a).T to get a column.
Does NumPy transpose copy the data?
Not normally. A view is returned whenever possible, so the result shares memory with the original. Call .copy() for independent data.
How do I transpose only the last two axes of a 3-D array?
Use np.matrix_transpose(x), or np.swapaxes(x, -1, -2). A plain x.T would reverse all three axes.
Sources
- NumPy documentation: numpy.transpose, checked 2026-10-29.
- NumPy documentation: numpy.ndarray.T, checked 2026-10-29.
- NumPy documentation: numpy.matrix_transpose, checked 2026-10-29.
- NumPy documentation: numpy.swapaxes, checked 2026-10-29.
- NumPy: Installing NumPy, checked 2026-10-29.
- pandas documentation: pandas.DataFrame.T, checked 2026-10-29.








