For a 2D array, .T swaps rows and columns, turning a shape of (m, n) into (n, m) — element (i, j) in the original array becomes element (j, i) in the transposed one. For a 1D array, .T has no effect, since there's only one axis to reverse. Like most NumPy attribute-based operations, .T returns a view of the original data rather than a copy whenever possible, meaning modifying the transposed array's elements also modifies the original array's underlying data.
1Understanding ndarray.T
For a 2D array, .T swaps rows and columns, turning a shape of (m, n) into (n, m) — element (i, j) in the original array becomes element (j, i) in the transposed one. For a 1D array, .T has no effect, since there's only one axis to reverse. Like most NumPy attribute-based operations, .T returns a view of the original data rather than a copy whenever possible, meaning modifying the transposed array's elements also modifies the original array's underlying data.
arr.T is a view, not a copy — modifying elements through the transposed array changes the original array's data too, which is efficient but can be a surprising source of bugs if you expected an independent copy.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.T)2Practical Example
Here is a real-world application of ndarray.T showing how it is used in production NumPy code.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
transposed = matrix.T
transposed[0, 0] = 99
print(matrix)3Best Practices
Follow these guidelines when working with ndarray.T:
1. Use .T for a quick transpose of a 2D array instead of manually swapping axes with np.transpose() when you don't need to specify a custom axis order
2. Call .copy() on the result of .T explicitly when you need an independent transposed array that won't affect the original if modified
3. Use np.transpose() instead of .T for arrays with more than 2 dimensions, where 'transpose' needs an explicit axis order rather than a simple reversal
Tip: arr.T is a view, not a copy — modifying elements through the transposed array changes the original array's data too, which is efficient but can be a surprising source of bugs if you expected an independent copy.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.T)