.transpose()

The .transpose() method reverses or permutes the axes of an ndarray.

Syntax

numpy.transpose(axes)

Where axes is a tuple or list with as many entries as there are dimensions, listing the 0-based indicies of each index in the new order in which they’ll appear in the permuted array. If axes is omitted, .transpose() will simply reverse the order of axes.

Unlike the built-in .transpose() function, the .transpose() method can accept the axes as separate arguments, as well as in the form of a tuple: i.e. array.transpose(2,0,1) is the same as array.transpose((2,0,1)).

If possible, the ndarray returned will be a view of the original ndarray‘s data.

Example

The below example creates an ndarray and then uses .transpose() on it.

import numpy as np
nd1 = np.array([[1,2,3],[4,5,6]])
print(nd1)
print(nd1.transpose())

This produces the following output:

[[1 2 3]
[4 5 6]]
[[1 4]
[2 5]
[3 6]]
Interested in helping build Docs? Read the Contribution Guide or share your feedback form.

Learn Python:NumPy on Codecademy