×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Concatenate a NumPy array to another NumPy array

Concatenation of NumPy Arrays: In this tutorial, we will learn how to concatenate a NumPy array to another NumPy array with the help of examples. By Pranit Sharma Last updated : June 04, 2023

Problem Statement

Given two or more NumPy arrays of the same shapes, we have to concatenate them into one NumPy array.

Solution: How to concatenate a NumPy array to another NumPy array?

To concatenate a NumPy array to another NumPy array, you can use the numpy.concatenate() method by passing the given arrays. This will return an array by concatenating the given arrays.

Example 1: Concatenate two one-dimensional NumPy arrays

Consider the below-given program to concatenate two given 1D NumPy arrays.

# Import numpy
import numpy as np

# Create two 1D numpy arrays
arr1 = np.array([10, 20, 30, 40, 50])
arr2 = np.array([60, 70, 80, 90, 100])

# Printing the arrays
print("arr1\n", arr1)
print("arr2\n", arr2)

# Concatenate these given arrays
result = np.concatenate((arr1, arr2))

# Printing the result
print("Concatenated array (result):\n", result)

Output

arr1
 [10 20 30 40 50]
arr2
 [ 60  70  80  90 100]
Concatenated array (result):
 [ 10  20  30  40  50  60  70  80  90 100]

Example 2: Concatenate two two-dimensional NumPy arrays

Consider the below-given program to concatenate two given 2D NumPy arrays.

# Import numpy
import numpy as np

# Create two 2D numpy arrays
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[9, 8, 7], [6, 5, 4]])

# Printing the arrays
print("arr1\n", arr1)
print("arr2\n", arr2)

# Concatenate these given arrays
result = np.concatenate((arr1, arr2))

# Printing the result
print("Concatenated array (result):\n", result)

Output

arr1
 [[1 2 3]
 [4 5 6]]
arr2
 [[9 8 7]
 [6 5 4]]
Concatenated array (result):
 [[1 2 3]
 [4 5 6]
 [9 8 7]
 [6 5 4]]

Python NumPy Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.