numpy.ndarray.transpose — NumPy v2.6.dev0 Manual

Learn | NEPs | User Guide | API reference | Building from source | Development | Release notes | NumPy’s module structure | Array objects | The N-dimensional array (ndarray) | Scalars | Data type objects (dtype) | Data type promotion in NumPy | Iterating over arrays | Standard array subclasses | Masked arrays | The array interface protocol | Datetimes and timedeltas | Universal functions (ufunc) | Routines and objects by topic | Typing (numpy.typing) | NumPy C-API | Array API standard compatibility | CPU/SIMD optimizations | Thread Safety | Global Configuration Options | NumPy security | Testing guidelines

method

ndarray.transpose(*axes)

#

Returns a view of the array with axes transposed.

Refer to

numpy.transpose

for full documentation.

Parameters:axesNone, tuple of ints, or n intsNone or no argument: reverses the order of the axes.

tuple of ints: i in the j-th place in the tuple means that the array’s i-th axis becomes the transposed array’s j-th axis.

n ints: same as an n-tuple of the same ints (this form is intended simply as a “convenience” alternative to the tuple form).

Returns:pndarrayView of the array with its axes suitably permuted.

Examples

>>> importnumpyasnp>>> a=np.array([[1,2],[3,4]])>>> aarray([[1, 2], [3, 4]])>>> a.transpose()array([[1, 3], [2, 4]])>>> a.transpose((1,0))array([[1, 3], [2, 4]])>>> a.transpose(1,0)array([[1, 3], [2, 4]])>>> a=np.array([1,2,3,4])>>> aarray([1, 2, 3, 4])>>> a.transpose()array([1, 2, 3, 4])

1 1