With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array.
Parameters:
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 than remember only pass (0, 1) or (1, 0). Like we have array of shape (2, 3) to change it (3, 2) you should pass (1, 0) where 1 as 3 and 0 as 2.Returns: ndarray
Example #1 :
In this example we can see that it’s really easy to transpose an array with just one line.
# importing python module named numpy import numpy as np # making a 3x3 array gfg = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # before transpose print(gfg, end ='\n\n') # after transpose print(gfg.transpose()) |
[[1 2 3] [4 5 6] [7 8 9]] [[1 4 7] [2 5 8] [3 6 9]]
Example #2 :
In this example we demonstrate the use of tuples in numpy.transpose().
# importing python module named numpy import numpy as np # making a 3x3 array gfg = np.array([[1, 2], [4, 5], [7, 8]]) # before transpose print(gfg, end ='\n\n') # after transpose print(gfg.transpose(1, 0)) |
[[1 2] [4 5] [7 8]] [[1 4 7] [2 5 8]]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Numpy numpy.resize()
- Python | Numpy numpy.ndarray.__lt__()
- Python | Numpy numpy.ndarray.__gt__()
- Python | Numpy numpy.ndarray.__le__()
- Python | Numpy numpy.ndarray.__ge__()
- Python | Numpy numpy.ndarray.__eq__()
- Python | Numpy numpy.ndarray.__ne__()
- Python | Numpy numpy.ndarray.__neg__()
- Python | Numpy numpy.ndarray.__pos__()
- Python | Numpy numpy.ndarray.__truediv__()
- Python | Numpy numpy.ndarray.__mul__()
- Python | Numpy numpy.ndarray.__sub__()
- Python | Numpy numpy.ndarray.__add__()
- Python | Numpy numpy.ndarray.__floordiv__()
- Python | Numpy numpy.ndarray.__mod__()
- Python | Numpy numpy.ndarray.__invert__()
- Python | Numpy numpy.ndarray.__divmod__()
- Python | Numpy numpy.ndarray.__pow__()
- Python | Numpy numpy.ndarray.__rshift__()
- Python | Numpy numpy.ndarray.__lshift__()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
