← Back to NumPy Course | Chapter 5: Array Manipulation | Lesson 3 of 7

transpose()

transpose() flips an array so its rows become columns and its columns become rows.

In this page:

  1. transpose()
Syntax
python
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()

python
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
  1. Expecting .T to turn a 1-D array into a column
  2. Forgetting the result is a view
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.