Signup/Sign In
LAST UPDATED: DECEMBER 7, 2020

NumPy transpose() function

In this tutorial, we will cover the numpy.transpose() function in the NumPy library of Python.

The transpose() function in the numpy library is mainly used to reverse or permute the axes of an array and then it will return the modified array.

  • The main task of this function is to change the column elements into the row elements and the column elements into the row elements.

  • This function has no effect on 1-D arrays and thus it is used for 2-D arrays.

Syntax of numpy.transpose():

The syntax required to use this function is as follows:

numpy.transpose(a, axes=None)

Parameters:

Let us take a look at the parameters of this function:

  • a
    This parameter indicates the input array

  • axes
    It is basically an optional parameter, but if you will pass this paramter then remember you should pass (0, 1) or (1, 0). Like we have array of shape (4, 3) to change it (3, 4) you should pass (1, 0) where 1 as 4 and 0 as 3.

Returned Values:

The transpose() function will return the input array with its permuted axes.

Example 1:

Let us have a look at the basic example of this function and the code snippet for the same is as follows:

import numpy as np 

a = np.array([[6, 2, 4], [8, 5, 6], [7, 1, 9]]) 
print("Before Transpose array is :\n",a) 
x=np.transpose(a)
print("After Transpose the array is:\n",x) 

numpy transpose function example

Example 2:

Now we will use transpose() function along with the axis parameter:

import numpy as np 

a = np.array([[6, 28], [8, 56], [7, 19]]) 
print("Before Transpose :\n",a) 
x=np.transpose(a,(1,0))
print("After Transpose :\n",x) 

numpy transpose function example

Summary:

This tutorial was all about transpose() function in the Numpy Library. We have covered how it is used with its syntax, its parameters and values returned by this function.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.