Combine Two Numpy Arrays: Steps and Methods

You must know that most the developers use numpy python package for creating numpy array for doing complex mathematical calculations. In this tutorial, you will know how to combine two numpy arrays with steps.

Step to Combine two numpy arrays

Let’s know all the processes of combining two NumPy arrays step by step using the numpy.concatenate method, which is the most used way to concatenate arrays along a specified axis.

Step 1: Import the NumPy library

import numpy as np

Step 2: Create the two NumPy arrays

array1 = np.array([10, 20, 30])
array2 = np.array([40, 50, 60])

Step 3: Use the numpy.concatenate

Use this function to combine the arrays along a specified axis. In this example, I am concatenating along the default axis (axis=0), which is suitable for 1-D arrays.

combined_array = np.concatenate((array1, array2))

If you want to concatenate along a different axis for 2-D arrays, you can specify the axis parameter. For example, to concatenate along columns you will use (axis=1)

combined_array = np.concatenate((array1.reshape(-1, 1), array2.reshape(-1, 1)), axis=1)

Step 4: Print the array

print(combined_array)

Below is the complete code

import numpy as np

# Create two arrays
array1 = np.array([10, 20, 30])
array2 = np.array([40, 50, 60])

# Concatenate along the default axis (axis=0)
combined_array = np.concatenate((array1, array2))

# Print or use the combined array
print(combined_array)

Below is the output

[10 20 30 40 50 60]

The above are the steps for combining two numpy arrays using the one method. Lets know all the methods for combining arrays.

Other Methods to combine two numpy arrays

1. Concatenation using numpy.concatenate (Already discussed in the above step 3)

import numpy as np

# Create two arrays
array1 = np.array([10, 20, 30])
array2 = np.array([40, 50, 60])

# Concatenate along an existing axis (default is 0 for 1-D arrays)
combined_array = np.concatenate((array1, array2))

print(combined_array)

Output

[10 20 30 40 50 60]

2. Vertical stacking using numpy.vstack:

import numpy as np

# Create two arrays
array1 = np.array([10, 20, 30])
array2 = np.array([40, 50, 60])

# Stack arrays vertically
combined_array = np.vstack((array1, array2))

print(combined_array)

Output

[10 20 30 40 50 60]

3. Horizontal stacking using numpy.hstack

import numpy as np

# Create two arrays
array1 = np.array([10, 20, 30])
array2 = np.array([40, 50, 60])
# Stack arrays horizontally
combined_array = np.hstack((array1, array2))

print(combined_array)

Output

[10 20 30 40 50 60]

4. Using numpy.append

You can use the numpy.append() function to just append the other numpy arrays from the end of the array.

import numpy as np

# Create two arrays
array1 = np.array([10, 20, 30])
array2 = np.array([40, 50, 60])

# Append the second array to the first
combined_array = np.append(array1, array2)

print(combined_array)

Output

[10 20 30 40 50 60]

5. Using numpy.column_stack for 1-D arrays:

import numpy as np

# Create two arrays
array1 = np.array([10, 20, 30])
array2 = np.array([40, 50, 60])

# Stack 1-D arrays as columns
combined_array = np.column_stack((array1, array2))

print(combined_array)

Output

[10 20 30 40 50 60]

Conclusion

There are many use cases for combining two numpy arrays. Like you take one array of datasets and combine it with the other dataset. The above are simple steps to combine the array. In this post, you have also learned all the methods to combine two numpy arrays

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link