Recently, I was working on a data science project where I needed to transform a flat array into a multi-dimensional structure for matrix operations. The issue is, reshaping arrays manually can be tedious and error-prone. So we need NumPy’s useful reshaping capabilities.
In this article, I’ll cover several simple ways you can use to reshape arrays in Python using NumPy.
So let’s dive in!
Reshape Arrays in NumPy
When working with data in Python, we often need to change the structure of our arrays to make them compatible with various algorithms or to better visualize patterns in our data. NumPy makes this process easy with its reshape functions.
Before we get started, let’s import NumPy:
import numpy as npRead 3D Arrays in Python
Method 1 – Basic Reshaping with reshape()
The most common way to reshape an array is using the reshape() method in Python. This is perfect for converting between 1D and multi-dimensional arrays.
Here’s how to use it:
# Create a 1D array with 12 elements (months of the year)
months = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
# Reshape into a 3x4 array (quarters x months per quarter)
quarterly_view = months.reshape(3, 4)
print(quarterly_view)Output:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]You can refer to the screenshot below to see the output.

In this example, we’ve transformed our array from a linear list of months to a quarterly view where each row represents a quarter with 4 months.
You can also use negative values as placeholders, and NumPy will calculate the appropriate dimension:
# Let NumPy determine the number of rows
auto_rows = months.reshape(-1, 4)
print(auto_rows)
# Let NumPy determine the number of columns
auto_cols = months.reshape(3, -1)
print(auto_cols)Both will produce the same 3×4 result as before, but NumPy calculates the missing dimension automatically.
Check out Create a Python Empty Matrix
Method 2 – Use resize() for In-place Reshaping
While reshape() in Python, returns a new array. Sometimes, you want to modify the original array directly. That’s where resize() comes in:
# Create an array of US sales data (in thousands)
sales_data = np.array([145, 162, 178, 130, 112, 190, 174, 182, 173, 152, 120, 168])
# Resize in-place to a 4x3 array (quarters x months)
sales_data.resize(4, 3)
print(sales_data)Output:
[[145 162 178]
[130 112 190]
[174 182 173]
[152 120 168]]You can refer to the screenshot below to see the output.

The advantage here is that we don’t need to assign the result to a new variable – the original array is modified directly.
Read NumPy Reset Index of an Array in Python
Method 3 – Use ravel() and flatten() to Convert to 1D
Sometimes you need to convert a multi-dimensional array back to 1D in Python:
# Create a 3x4 array of US temperature data (°F)
temp_data = np.array([
[32, 36, 45, 55], # Q1 average temps
[65, 75, 82, 80], # Q2 average temps
[75, 68, 58, 42] # Q3 and Q4 average temps
])
# Convert to 1D using ravel() - returns a view
temp_flat_view = temp_data.ravel()
print(temp_flat_view)
# Convert to 1D using flatten() - returns a copy
temp_flat_copy = temp_data.flatten()
print(temp_flat_copy)Output:
[32 36 45 55 65 75 82 80 75 68 58 42]
[32 36 45 55 65 75 82 80 75 68 58 42]You can refer to the screenshot below to see the output.

Both methods produce a 1D array, but ravel() gives you a view of the original data, while flatten() creates a copy. This means changes to temp_flat_view will affect the original array, but changes to temp_flat_copy won’t.
Check out np.genfromtxt() Function in Python
Method 4 – Transpose an Array with transpose()
Transposing flips rows and columns, which is useful for many matrix operations:
# Create a 2x6 array of stock prices
stock_prices = np.array([
[145.23, 146.75, 144.80, 147.25, 150.10, 149.89], # Company A
[210.45, 208.36, 213.47, 215.20, 217.65, 220.12] # Company B
])
# Transpose from 2x6 to 6x2
transposed = np.transpose(stock_prices)
# Or alternatively: transposed = stock_prices.T
print(transposed)Output:
[[145.23 210.45]
[146.75 208.36]
[144.8 213.47]
[147.25 215.2 ]
[150.1 217.65]
[149.89 220.12]]Now, each row represents a time point, and each column represents a company.
Read np.savetxt() Function in Python
Method 5 – Advanced Reshaping with newaxis
You can add a new dimension using np.newaxis:
# Create an array of population data (in millions)
population = np.array([23.5, 39.6, 37.7, 29.1, 21.3])
# Add a new axis to convert 1D to 2D
column_vector = population[:, np.newaxis]
print(column_vector)
# Create a row vector instead
row_vector = population[np.newaxis, :]
print(row_vector)Output:
[[23.5]
[39.6]
[37.7]
[29.1]
[21.3]]
[[23.5 39.6 37.7 29.1 21.3]]This is particularly useful when working with broadcasting or when algorithms require specific dimensions.
Check out NumPy Array to List in Python
Method 6 – Reshape with reshape() and Order Parameter
NumPy allows you to control how elements are read during reshaping:
# Create a 3x4 array of GDP growth rates
gdp_growth = np.array([
[2.4, 2.9, 3.1, 3.5],
[3.2, 3.0, 2.7, 2.5],
[2.2, 1.8, 2.0, 2.3]
])
# Reshape using C-style ordering (row by row)
flat_c = gdp_growth.reshape(-1, order='C')
print("C-style (row-major):", flat_c)
# Reshape using Fortran-style ordering (column by column)
flat_f = gdp_growth.reshape(-1, order='F')
print("F-style (column-major):", flat_f)Output:
C-style (row-major): [2.4 2.9 3.1 3.5 3.2 3.0 2.7 2.5 2.2 1.8 2.0 2.3]
F-style (column-major): [2.4 3.2 2.2 2.9 3.0 1.8 3.1 2.7 2.0 3.5 2.5 2.3]The difference is in how the elements are arranged when flattened or reshaped – row by row (C-style) or column by column (Fortran-style).
Read NumPy Reverse Array in Python
Common Mistakes and How to Avoid Them
When reshaping arrays, remember these key points:
- Element count must match: The total number of elements must remain the same before and after reshaping.
# This will fail
try:
np.array([1, 2, 3, 4, 5]).reshape(2, 3)
except ValueError as e:
print(f"Error: {e}")- Use -1 wisely: Let NumPy calculate dimensions when possible.
- View vs. Copy: Remember that
reshape()creates a view by default, whileflatten()creates a copy.
I hope you found this article helpful.
Reshaping arrays is a fundamental skill for data manipulation in NumPy, and these techniques will serve you well in your data science projects. Whether you’re preparing data for a machine learning model or transforming results for visualization, knowing how to efficiently reshape your arrays will make your code cleaner and more efficient.
Other Python articles you may also like:
- Replace Values in NumPy Array by Index in Python
- np.add.at() Function in Python
- NumPy Array to a String in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.