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.

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.

**📚 Discover Online Data Science Courses & Programs (Enroll for Free)**

**Introductory ⭐**

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 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.

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.

## Numpy Transpose 1d array

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 –

**Upskill your career right now →**

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:

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

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

## Numpy Transpose 2d array

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 –

**Upskill your career right now →**

# 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.**