transpose()
transpose() flips an array so its rows become columns and its columns become rows.
In this page:
Syntax
arr.transpose()
arr.T
np.transpose(arr, axes)
transpose()
For a 2-D array, transpose swaps the two axes; the shortcut is the .T attribute. For higher dimensions you can pass a custom axis order. The result is a view of the same data.
Note:
A 1-D array is unchanged by .T; reshape it into a 2-D array first.
Example: transpose()
import numpy as np
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.shape)
print(m.T)
print(m.T.shape)
# Output:
# (2, 3)
# [[1 4]
# [2 5]
# [3 6]]
# (3, 2)
Related Topics
Common Mistakes
- Expecting .T to turn a 1-D array into a column
- Forgetting the result is a view
- Using the wrong axes order for 3-D arrays
Chapter Summary
- .T swaps rows and columns
- np.transpose accepts an axes order
- Returns a view
- 1-D arrays do not change
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: