Transpose a 2d numpy array

Transpose a Numpy array

In this tutorial, we will look at how to transpose a numpy array with the help of some examples. Speicifically, we will look at the usage of the numpy ndarray transpose() function and the numpy ndarray .T attribute.

Transpose a 2d array in numpy

The transpose operation in numpy is generally applied on 2d arrays to swipe the rows and columns of an array. For example, a numpy array of shape (2, 3) becomes a numpy array of shape (3, 2) after the operation wherein the first row becomes the first column and the second row becomes the second column. Also, conversely, the first column becomes the first row, the second column becomes the second row, and the third column becomes the third row post the transpose.

You can use the numpy ndarary transpose() function to transpose a numpy array. You can also use the .T numpy array attribute to transpose a 2d array. The following is the syntax:

# arr is a numpy array
arr_t = arr.transpose()

It returns a view of the array with the axes transposed.

For 1d arrays, the transpose operation has no effect on the array. As a transposed vector it is simply the same vector. Let’s see an example –

import numpy as np

# create a 1d numpy array
arr = np.array([1, 2, 3, 4])
# transpose the array
arr_t = arr.transpose()
# display the arrays
print("Original Array:\n", arr)
print("After Transpose:\n", arr_t)

Output:

Original Array:
 [1 2 3 4]
After Transpose:
 [1 2 3 4]

You can see that transposing a 1d array doesn’t change anything. We can further confirm this by looking at the shape of the two arrays:

# print the shape of the two arrays
print("Original array shape: ", arr.shape)
print("Shape after transpose: ", arr_t.shape)

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Original array shape:  (4,)
Shape after transpose:  (4,)

Both the arrays (original and transposed) have the same shape.

For a 2d array, the transpose operation means to swap out the rows and columns of the array. Here’s an example –

# create a 2d numpy array
arr = np.array([[1, 2, 3],
                [4, 5, 6]])
# transpose the array
arr_t = arr.transpose()
# display the arrays
print("Original Array:\n", arr)
print("After Transpose:\n", arr_t)

Output:

Original Array:
 [[1 2 3]
 [4 5 6]]
After Transpose:
 [[1 4]
 [2 5]
 [3 6]]

Here, we transpose a 2×3 array. Note that after the transpose, the first row in the original array [1, 2, 3] becomes the first column in the transposed array and similarly the second row [4, 5, 6] becomes the second column in the transposed array. Let’s look at the shape of the two arrays –

# print the shape of the two arrays
print("Original array shape: ", arr.shape)
print("Shape after transpose: ", arr_t.shape)

Output:

Original array shape:  (2, 3)
Shape after transpose:  (3, 2)

Alternatively, you can swipe the rows and columns of a numpy array using the .T attribute.

# create a 2d numpy array
arr = np.array([[1, 2, 3],
                [4, 5, 6]])
# transpose the array
arr_t = arr.T
# display the arrays
print("Original Array:\n", arr)
print("After Transpose:\n", arr_t)

Output:

Original Array:
 [[1 2 3]
 [4 5 6]]
After Transpose:
 [[1 4]
 [2 5]
 [3 6]]

We get the same result as we did with the numpy ndarray transpose() function.

For more on the numpy ndarray transpose function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top