Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallFor a two-dimensional NumPy array, use a.T to exchange rows and columns. NumPy also offers transpose(), np.transpose(), swapaxes() and moveaxis() for more explicit axis control. For a plain rectangular list of lists, use zip(*matrix). The right choice depends on the data type and, for arrays with more than two dimensions, which axes you want to rearrange.
Transpose a 2D NumPy array
Start with a non-square array so it is easy to see that rows become columns and columns become rows:
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
print(a.shape) # (2, 3)
print(a.T)
# [[1 4]
# [2 5]
# [3 6]]
The result has shape (3, 2). These three forms perform the same full transpose on a 2D NumPy array:
a_t = a.T
a_t = a.transpose()
a_t = np.transpose(a)
1. Use the .T property
a.T is the concise, commonly used form for an ndarray. On a 2D array, it exchanges the two axes. NumPy documents .T as equivalent to the ndarray transpose method: ndarray.T.
#1 Best Overall
2. Call ndarray.transpose()
Use a.transpose() when a method call fits more clearly into a transformation pipeline. With no axis order supplied, it reverses the order of all axes for an n-dimensional array. NumPy returns a view where possible; see the ndarray.transpose documentation.
3. Call np.transpose()
The function form works on an array and accepts an optional axis permutation. For example, if a 3D array has axes (0, 1, 2), np.transpose(a, (1, 0, 2)) exchanges the first two axes while leaving the third in place. The supplied axes must be a permutation of the input axes; negative axis indices are also accepted. See numpy.transpose.
Choose the right axis operation for multidimensional data
For a 2D array, swapping its two axes is the familiar transpose. With three or more dimensions, be specific: NumPy’s default transpose reverses the entire axis order, which may not be the same as swapping just two axes.
Rank #2
Full reversal versus a specific permutation
For an array of shape (2, 3, 4), a default transpose produces shape (4, 3, 2). To choose another arrangement, pass the complete desired axis order to np.transpose(), such as (1, 0, 2) to exchange the first two axes and keep the third fixed.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →4. Exchange two axes with swapaxes()
Use np.swapaxes(a, axis1, axis2) when the goal is specifically to exchange a named pair of axes. For a 2D array, np.swapaxes(a, 0, 1) gives the same row-and-column exchange as a.T. See NumPy’s axis-manipulation documentation.
5. Move axes with moveaxis()
Use np.moveaxis(a, source, destination) to move selected source axes to destination positions. Unlike a full axis reversal, it keeps the other axes in their relative order. For a 2D input, np.moveaxis(a, 0, 1) also yields the familiar transpose. Choose swapaxes() for an exchange and moveaxis() for a relocation.
Transpose a plain list of lists
You do not need NumPy for a rectangular nested list. Python’s zip(*matrix) idiom groups elements from each row into columns:
matrix = [[1, 2, 3],
[4, 5, 6]]
transposed = list(zip(*matrix))
print(transposed)
# [(1, 4), (2, 5), (3, 6)]
The result contains tuples. Convert them to lists if the output needs to remain a list of lists:
transposed = [list(row) for row in zip(*matrix)]
# [[1, 4], [2, 5], [3, 6]]
The Python tutorial demonstrates this idiom; the Python built-ins documentation describes zip() as turning rows into columns and columns into rows.
Watch for unequal row lengths
By default, zip() stops when its shortest input is exhausted. If the nested lists have unequal lengths, values remaining in longer rows are omitted. In Python 3.10 and later, strict=True makes zip() raise ValueError when input lengths differ:
transposed = list(zip(*matrix, strict=True))
Use strict mode when unequal row lengths indicate invalid input rather than something to silently truncate.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.What happens with 1D arrays, views and DataFrames?
A 1D NumPy array stays one-dimensional
Transposing a one-dimensional ndarray does not create a row or column vector: np.transpose(a) returns it unchanged in shape. Add an axis when you need a column vector:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsBest Value
column = a[:, np.newaxis]
# Or:
column = np.atleast_2d(a).T
NumPy documents this behavior in numpy.transpose.
A NumPy transpose may share storage
NumPy returns a view whenever possible, so do not assume the transposed result has independent storage. If you need a separate copy, request one explicitly, for example with a.T.copy(). See the transpose documentation and ndarray.T documentation.
Transpose a pandas DataFrame
For a DataFrame, use df.T or df.transpose() to exchange its index and columns. When the DataFrame contains mixed dtypes, the transposed frame has a homogeneous object dtype. In pandas 3.0, the copy argument is ignored and deprecated; lazy Copy-on-Write behavior is used, and a copy is always required for mixed-dtype DataFrames or extension types. Consult the pandas DataFrame.transpose documentation for the version-specific behavior.
Quick Recap
Quick method guide
| Data or goal | Recommended form | Important detail |
|---|---|---|
| 2D NumPy array; concise row/column exchange | a.T |
Equivalent to a.transpose() for this use. |
| NumPy array; specify output axis order | np.transpose(a, axes=...) |
Provide a permutation of all input axes. |
| Exchange two selected NumPy axes | np.swapaxes(a, axis1, axis2) |
Only the named pair is exchanged. |
| Move selected NumPy axes | np.moveaxis(a, source, destination) |
Other axes retain their relative order. |
| pandas DataFrame | df.T or df.transpose() |
Mixed dtypes yield an object-dtype transposed frame. |
| Rectangular nested list | list(zip(*matrix)) |
Produces tuples; unequal rows truncate unless strict mode is used. |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




