HOWTO · NumPy
How to Transpose a 1D Array in NumPy
Learn how to transpose a 1D NumPy array in Python by reshaping it into a 2D format. This article covers practical methods using NumPy's reshape and newaxis functions, providing clear examples and explanations. Perfect for data manipulation in Python, especially in data science and machine learning contexts. Discover tips and techniques to enhance your array handling skills today!
On this page
Transposing a 1D array in NumPy is a common point of confusion: calling .T on a one-dimensional ndarray runs without complaint, yet the array comes back looking exactly the same. To get a real transpose, you first promote the 1D array to a 2D row or column vector and transpose that. This article explains why .T leaves a 1D array alone and shows the standard ways to add the second axis.
Why .T leaves a 1D array unchanged
A 1D ndarray has a shape of (n,): a single axis holding n elements. Transposing swaps the order of an array’s axes, and with only one axis there is nothing to swap, so .T hands back the same data with the same shape. Check the shape before and after:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a.shape)
print(a.T.shape)
print(a.T)
Output:
(5,)
(5,)
[1 2 3 4 5]
The shape (5,) and the values are identical after .T, which is why people say you cannot transpose a 1D array. It is more precise to say the transpose succeeds but is a no-op: NumPy returns a view over the same one-dimensional data.
Keep in mind that .T is an ndarray attribute, not a general Python feature. A plain list has no .T, so convert list data with np.array() or np.asarray() before doing any of this.
Create a row or column vector
The practical fix is to give the array a second axis. A row vector has shape (1, n), a column vector has shape (n, 1), and both transpose the way you would expect. The reshape method produces a row vector like this:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
row = a.reshape(1, -1)
print(row.shape)
print(row)
Output:
(1, 5)
[[1 2 3 4 5]]
The 1 fixes the number of rows; the -1 tells NumPy to compute the column count so all five elements fit. The same row results from a[np.newaxis, :], which inserts a new axis at the front while indexing.
For a column vector, swap the arguments:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
col = a.reshape(-1, 1)
print(col.shape)
print(col)
Output:
(5, 1)
[[1]
[2]
[3]
[4]
[5]]
The equivalent np.newaxis form is a[:, np.newaxis], which keeps the existing axis in place and appends a new one of length 1. Note the double brackets in the printed output: they signal a 2D array, as opposed to the single brackets of a 1D array.
Compare reshape and np.newaxis
Both tools add an axis without copying data, so the choice is about readability rather than performance. The pairs are equivalent:
a.reshape(1, -1)produces the same row vector asa[np.newaxis, :].a.reshape(-1, 1)produces the same column vector asa[:, np.newaxis].
Reach for reshape when the target shape is the point of the call: it spells out the dimensions you want and can rearrange several axes in one step. Reach for np.newaxis inside an index when you are already slicing, or when you want the axis insertion to be visible at the exact position it happens. Since np.newaxis is an alias for None, writing a[None, :] works identically. You can confirm the pairs produce identical results:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(np.array_equal(a.reshape(1, -1), a[np.newaxis, :]))
print(np.array_equal(a.reshape(-1, 1), a[:, np.newaxis]))
Output:
True
True
Verify the shapes and outputs
Once the array is 2D, .T does what most readers actually want: it turns a row vector into a column vector and a column vector back into a row. Run this complete check and compare the reported shapes with the printed values:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
row = a[np.newaxis, :]
col = row.T
print("a:", a.shape)
print("row:", row.shape)
print("row.T:", col.shape)
print(col)
Output:
a: (5,)
row: (1, 5)
row.T: (5, 1)
[[1]
[2]
[3]
[4]
[5]]
The values are untouched; only the axis order differs. Use .shape (or .ndim) whenever you are unsure which object you hold: a printed (5,) means the array is still 1D and .T will leave it as is, while (1, 5) or (5, 1) means you have a 2D vector that transposes. This distinction matters for downstream work such as matrix multiplication and model input preparation, which expect two-dimensional input.