numpy.transpose() – The NumPy transpose Python Function

Introduction to NumPy transpose Python Function

Guide to NumPy transpose Python Function – We may do the simple function of transposing within one line by utilizing Numpy’s numpy.transpose() function. Although it can transpose 2-D arrays, it has no effect on 1-D arrays. The 2-D NumPy array is transposed using this method.


numpy.transpose(a, axes=None)


  • array: The array is pass as a Parameter.
  • axes : [None, tuple of ints, or n ints] If anyone wants to pass the parameter then you can but it’s not all required. But if you want then remember to only pass (0, 1) or (1, 0). Like we have an array of shape (2, 3) to change it (3, 2) you should pass (1, 0) where 1 as 3 and 0 as 2.

Return: It returns ndarray.

Code Examples of NumPy transpose Python Function

Example 01: We can see how simple it is to transpose an array with only one line in this example.

# welcome to
# importing python module named numpy
import numpy as np

# making a 3x3 array
arr = np.array([[1, 4, 7],
				[2, 5, 8],
				[3, 6, 9]])

# before transpose
print('Original array:\n', arr, end ='\n\n')

# after transpose
print('Transposed array:\n', arr.transpose())


Original array:
 [[1 4 7]
 [2 5 8]
 [3 6 9]]

Transposed array:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]

Example 02: In this example, we demonstrate the use of tuples in numpy.transpose().

# welcome to
# importing python module named numpy
import numpy as np

# making a 3x3 array
arr = np.array([[1, 2],
				[4, 5],
				[7, 8]])

# before transpose
print("Before transpose:\n", arr, end ='\n\n')

# after transpose
print("After transpose:\n", arr.transpose(1, 0))


Before transpose:
 [[1 2]
 [4 5]
 [7 8]]

After transpose:
 [[1 4 7]
 [2 5 8]]


How do I flip columns and rows in NumPy?

By Using the T attribute (.T), the ndarray method transpose(), and the numpy.transpose() function to transpose a NumPy array ndarray (swap rows and columns).

You may not only transpose a 2D array (matrix) but also rearrange the axes of a multi-dimensional array in any order using ndarray.transpose() and numpy.transpose().

Can you transpose an array Python?

The transpose of a matrix is generate by transferring the data from the rows to the columns and the data from the columns to the rows. If we have an array of shape (X, Y), the array’s transposition will have the same form (Y, X). It is done by transpose() function.


That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.ndarray.T – The NumPy ndarray.T

Leave a Comment