Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Transpose an Array in Python: 5 Methods with Examples

Use .T for a 2D NumPy array, explicit axis operations for multidimensional data, or zip(*matrix) for a plain rectangular list of lists.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.Support on Ko-Fi

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.