python-scipyHow do I join two arrays in Python using NumPy?
Joining two arrays in Python using NumPy is a fairly straightforward process. It can be done using the np.concatenate()
function. This function takes a tuple or list of arrays to join together.
For example, if we have two NumPy arrays a
and b
, we can join them together using np.concatenate()
like this:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.concatenate((a, b))
print(c)
The output of the above code would be:
[1 2 3 4 5 6]
The np.concatenate()
function takes the following parameters:
tuple
orlist
of arrays to join togetheraxis
: The axis along which the arrays will be joined. If it is not specified, it defaults to 0.
Code explanation
import numpy as np
: Imports the NumPy library.a = np.array([1, 2, 3])
: Creates a NumPy arraya
with the values[1, 2, 3]
.b = np.array([4, 5, 6])
: Creates a NumPy arrayb
with the values[4, 5, 6]
.c = np.concatenate((a, b))
: Joins the arraysa
andb
together usingnp.concatenate()
.print(c)
: Prints the resulting arrayc
.
Helpful links
More of Python Scipy
- How can I use Python Scipy to zoom in on an image?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I use Python and Numpy to parse XML data?
- How do I calculate variance using Python and SciPy?
- How do I download a Python Scipy .whl file?
- How do I integrate Scipy with Python?
- How do I use the numpy vstack function in Python?
See more codes...