Create a 2D NumPy Array in Python (5 Simple Methods)

When I was working on a data analysis project, I needed to organize and process a large dataset of customer information for a US retail chain. I quickly realized that using Python lists wasn’t efficient enough for the complex calculations I needed to perform. That’s when I turned to NumPy arrays, particularly 2D arrays, which dramatically improved my workflow.

In this article, I’ll show you five easy methods to create 2D NumPy arrays (also known as matrices) based on my decade of experience working with Python.

These techniques will help you efficiently organize and manipulate your data, whether you’re analyzing sales figures, processing image data, or building machine learning models.

Create a 2D NumPy Array in Python

Now, I will explain to you how to create a 2D NumPy array in Python.

Method 1 – Use numpy.array() Function

The most common way to create a 2D NumPy array is by using the numpy.array() function. This method converts nested Python lists into a multidimensional array.

import numpy as np

# Creating a 2D array from a list of lists
sales_data = np.array([
    [1200, 1500, 1100],  # Q1 sales for 3 products
    [1300, 1200, 1400],  # Q2 sales for 3 products
    [1500, 1400, 1600]   # Q3 sales for 3 products
])

print(sales_data)
print("Shape:", sales_data.shape)
print("Dimensions:", sales_data.ndim)

Output:

[[1200 1500 1100]
 [1300 1200 1400]
 [1500 1400 1600]]
Shape: (3, 3)
Dimensions: 2

Refer to the screenshot below to see the output.

numpy 2d array

The shape attribute tells us this is a 3×3 array, and ndim confirms it’s a 2D array. This method is perfect when you already have your data structured in lists.

Read Python NumPy Not Found: Fix Import Error

Method 2 – Use numpy.zeros(), ones(), and full()

When you need to initialize arrays with specific values, NumPy provides several convenient functions.

import numpy as np

# Create a 2D array filled with zeros
zeros_array = np.zeros((3, 4))

# Create a 2D array filled with ones
ones_array = np.ones((2, 5))

# Create a 2D array filled with a specific value
filled_array = np.full((2, 3), 99)

print("Zeros array:\n", zeros_array)
print("\nOnes array:\n", ones_array)
print("\nCustom filled array:\n", filled_array)

Output:

Zeros array:
 [[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

Ones array:
 [[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]

Custom filled array:
 [[99 99 99]
 [99 99 99]]

Refer to the screenshot below to see the output.

2d array in python

I often use these functions when initializing arrays for accumulating results or creating placeholder data structures.

Check out Create a Matrix in Python

Method 3 – Use numpy.arange() and reshape()

For arrays with sequential values, numpy.arange() combined with reshape() offers an efficient approach.

import numpy as np

# Create a 1D array with sequential numbers
numbers = np.arange(12)  # Creates [0, 1, 2, ..., 11]

# Reshape it into a 2D array
reshaped_array = numbers.reshape(4, 3)

# You can also do this in one step
direct_reshaped = np.arange(15).reshape(3, 5)

print("Original 1D array:", numbers)
print("\nReshaped to 2D (4×3):\n", reshaped_array)
print("\nDirect reshape (3×5):\n", direct_reshaped)

Output:

Original 1D array: [ 0  1  2  3  4  5  6  7  8  9 10 11]

Reshaped to 2D (4×3):
 [[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

Direct reshape (3×5):
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

Refer to the screenshot below to see the output.

create 2d array python

The total number of elements in the reshaped array must match the original array’s size. This method is great for creating test data or generating sequential indices.

Read Random Number Between Two Values in Numpy

Method 4 – Use numpy.random Functions

Python NumPy’s random module is my go-to solution when I need to create arrays with random values (perfect for simulations or testing algorithms).

import numpy as np

# Create a 2D array with random values between 0 and 1
random_uniform = np.random.rand(3, 4)

# Create a 2D array with standard normal distribution
random_normal = np.random.randn(2, 3)

# Create a 2D array with random integers
random_integers = np.random.randint(1, 100, size=(2, 5))

print("Random uniform (0-1):\n", random_uniform)
print("\nRandom normal distribution:\n", random_normal)
print("\nRandom integers (1-99):\n", random_integers)

Output will vary each time, but will look something like:

Random uniform (0-1):
 [[0.23651224 0.41900661 0.84329673 0.99959938]
 [0.18532822 0.10837689 0.21969749 0.97323448]
 [0.6071983  0.38097698 0.77714806 0.07964548]]

Random normal distribution:
 [[ 0.97861834  1.79248043 -0.24374806]
 [-0.81979725 -0.87810045  0.19544158]]

Random integers (1-99):
 [[62 38 45 12 44]
 [68 43 97 16 39]]

I’ve used this method extensively for creating test datasets, initializing neural network weights, and running Monte Carlo simulations.

Check out Create a Python Empty Matrix

Method 5 – Use numpy.eye() for Identity Matrix

For certain mathematical operations, you might need an identity matrix (with 1s on the diagonal and 0s elsewhere).

import numpy as np

# Create a 4×4 identity matrix
identity_matrix = np.eye(4)

# Create a 3×5 matrix with the diagonal of 1s
rectangular_eye = np.eye(3, 5)

# Create a matrix with the diagonal shifted
offset_eye = np.eye(4, k=1)  # Offset by 1

print("Identity matrix (4×4):\n", identity_matrix)
print("\nRectangular eye matrix (3×5):\n", rectangular_eye)
print("\nOffset diagonal (4×4, k=1):\n", offset_eye)

Output:

Identity matrix (4×4):
 [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

Rectangular eye matrix (3×5):
 [[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]]

Offset diagonal (4×4, k=1):
 [[0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]
 [0. 0. 0. 0.]]

This method is invaluable when working with linear algebra operations or when you need a specialized initialization pattern.

Read NumPy Reset Index of an Array in Python

Practical Example: Analyze US Sales Data

Let’s apply these methods to a real-world example. Imagine you’re analyzing quarterly sales data for different regions in the US.

import numpy as np

# Create a 2D array for quarterly sales data (rows: quarters, columns: regions)
sales_data = np.array([
    [250000, 320000, 180000, 310000],  # Q1 sales for 4 regions
    [270000, 300000, 195000, 340000],  # Q2 sales
    [310000, 350000, 220000, 370000],  # Q3 sales
    [390000, 420000, 290000, 450000]   # Q4 sales
])

# Region names and quarters for reference
regions = ['East', 'West', 'South', 'North']
quarters = ['Q1', 'Q2', 'Q3', 'Q4']

# Calculate total sales per region
region_totals = np.sum(sales_data, axis=0)

# Calculate total sales per quarter
quarter_totals = np.sum(sales_data, axis=1)

# Find the region with maximum sales
best_region_index = np.argmax(region_totals)
best_region = regions[best_region_index]

# Find the quarter with maximum sales
best_quarter_index = np.argmax(quarter_totals)
best_quarter = quarters[best_quarter_index]

print("Sales data (quarters × regions):\n", sales_data)
print("\nTotal sales per region:", region_totals)
print("Total sales per quarter:", quarter_totals)
print(f"\nBest performing region: {best_region} with ${region_totals[best_region_index]:,} in sales")
print(f"Best performing quarter: {best_quarter} with ${quarter_totals[best_quarter_index]:,} in sales")

Output:

Sales data (quarters × regions):
 [[250000 320000 180000 310000]
 [270000 300000 195000 340000]
 [310000 350000 220000 370000]
 [390000 420000 290000 450000]]

Total sales per region: [1220000 1390000 885000 1470000]
Total sales per quarter: [1060000 1105000 1250000 1550000]

Best performing region: North with $1,470,000 in sales
Best performing quarter: Q4 with $1,550,000 in sales

This example demonstrates how 2D NumPy arrays make it easy to organize, analyze, and extract insights from structured data.

I hope you found this guide helpful for creating and working with 2D NumPy arrays. Whether you’re analyzing sales data, processing images, or building machine learning models, these methods provide a solid foundation for your data manipulation needs. If you have any questions or suggestions, please let me know in the comments below.

Other Python articles you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.