This content originally appeared on DEV Community and was authored by Ramya .C
NumPy Reshaping, Resizing, Stacking & Splitting Arrays
Today, I dived deep into NumPy, one of the most powerful Python libraries for data manipulation and numerical computing.
Here’s what I learned 👇
🔹 1️⃣ Reshaping Arrays
Reshaping means changing the shape (rows × columns) of an existing array without changing the data.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape(2, 3)
print(reshaped)
Output:
[[1 2 3]
[4 5 6]]
👉 The 1D array is reshaped into a 2D array (2 rows, 3 columns).
🔹 2️⃣ Resizing Arrays
Resizing creates a new array shape — it adds or removes elements to match the new size.
arr = np.array([1, 2, 3, 4])
resized = np.resize(arr, (2, 3))
print(resized)
Output:
[[1 2 3]
[4 1 2]]
Notice how elements repeat when needed to fill the new shape.
🔹 3️⃣ Stacking Arrays
Stacking means joining two or more arrays together.
👉 Horizontal Stacking
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.hstack((a, b)))
Output:
[[1 2 5 6]
[3 4 7 8]]
👉 Vertical Stacking
print(np.vstack((a, b)))
Output:
[[1 2]
[3 4]
[5 6]
[7 8]]
🔹 4️⃣ Splitting Arrays
Splitting means dividing an array into multiple sub-arrays.
arr = np.array([10, 20, 30, 40, 50, 60])
print(np.split(arr, 3))
Output:
[array([10, 20]), array([30, 40]), array([50, 60])]
🔹 5️⃣ Working with Different Dimensions
Understanding 1D, 2D, and 3D arrays is essential:
# 1D
arr1 = np.array([1, 2, 3])
# 2D
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
# 3D
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr1.ndim, arr2.ndim, arr3.ndim) # Output: 1 2 3
🚀 Key Takeaway
These NumPy operations help reshape and structure data efficiently — a critical skill for every Data Analyst or Data Scientist working with large datasets.
🏷️ Hashtags
DataAnalytics #Python #NumPy #MachineLearning #DataScience #LearningJourney
This content originally appeared on DEV Community and was authored by Ramya .C
Ramya .C | Sciencx (2025-10-27T16:08:22+00:00) 🎯 Day 54 of My Data Analytics Journey. Retrieved from https://www.scien.cx/2025/10/27/%f0%9f%8e%af-day-54-of-my-data-analytics-journey/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.