Python | Numpy numpy.transpose()
Last Updated :
10 Dec, 2025
The numpy.transpose() function is used to reverse or permute the axes of an array. For 2D arrays, it simply flips rows and columns. For 1D arrays, transpose has no effect because they have only one axis. This function is commonly used in matrix operations and data transformations where orientation matters.
Example: Here is a example showing how a 2D array is transposed
Python
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(np.transpose(a))
Syntax
numpy.transpose(a, axes=None)
Parameters:
- a: Input array to transpose
- axes (Optional): tuple that defines the new axis order (e.g., (1, 0) for swapping rows and columns)
Examples
Example 1: In this example, we transpose a 3×3 matrix using the default behavior of numpy.transpose(), which swaps row and column indices.
Python
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(np.transpose(a))
Output[[1 4 7]
[2 5 8]
[3 6 9]]
Explanation: np.transpose(a) swaps axes (0, 1) -> rows become columns.
Example 2: Here, we use the axes parameter to manually specify the new axis order, explicitly swapping the two axes of a 3×2 array.
Python
import numpy as np
a = np.array([[1, 2],
[3, 4],
[5, 6]])
print(np.transpose(a, (1, 0)))
Explanation: np.transpose(a, (1, 0)) forces column axis to come first and row axis to come second.
Example 3: This example demonstrates using the shorthand .T attribute, which provides the same result as numpy.transpose() for 2D arrays.
Python
import numpy as np
a = np.array([[10, 20, 30],
[40, 50, 60]])
print(a.T)
Output[[10 40]
[20 50]
[30 60]]
Explanation: a.T directly returns the transpose of the array by swapping axes (0, 1).
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice