Concatenating 2D- Arrays in Numpy in Python through Step by Step

Concatenating Numpy Arrays in Python

You will generally use to concatenate the operation of two or more data columns in the dataset. If you look out the dataset then each column is the data is NumPy arrays. In this article, you will know How to Concatenate Arrays in Numpy?

Concatenate Arrays in NumPy

Let’s understand the concatenate operation in small steps. But before it lets checkout the syntax –

numpy.concatenate((array1, array2, ...), axis)  

Here array1 and array2 are the arrays that are in use for concatenation. Here axis is an integer value. The default value of axis is 0(rows). You can use axis =1 for manipulate columns.

Step 1: Creation of Dummy Numpy Array

Let’s generate the NumPy array that we need to concatenate. This is for the demonstration purpose. You can use it on your arrays. But I will suggest first learn from it and then move to your dataset. Use the code given below.

import numpy as np  
array1=np.array([[11,12],[13,14]])  
array2=np.array([[31,32],[21,22]])

First I am importing the NumPy module first. Just after it, we have generated the two NumPy arrays with the name array1 and array2.

Step 2: Concatenate the Arrays

In this step, We will concatenate these arrays. Let’s see how –

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

Output –

Here is the complete code with output –

"<yoastmark

 

Important Notes –

1. If you need to concatenate in different axis, You may use the axis parameter. In the above example, we have not used the axis parameter while concatenation because we used the default axis =0. In case you want to change the axis for concatenation, Please refer the below example –

import numpy as np 
array1=np.array([[11,12],[13,14]]) 
array2=np.array([[31,32],[21,22]])
gen_array=np.concatenate((array1,array2),axis=1)
print(gen_array)

Output –

Numpy-concatenate-operation-axis1
concatenate-operation-axis1

 

 

 

 

 

 

Here you can see the concatenation is done column-wise.

2. You may concatenate multiple arrays at the same time. For example, I want to concatenate three arrays then I will pass all the three arrays as the argument to the numpy.concatenate().

concatenation-multiple-array
concatenation multiple arrays

 

 

 

 

 

 

 

 

Therefore We can concatenate ‘n’ numbers of arrays at the same time.

3. On the other hand, We may provide axis =None. Let’s see its impact –

"<yoastmark

It’s outputting only the distinct elements for the entire arrays used for concatenation.

Conclusion –

Numpy is one of the most popular python packages in data science and analytics stuff.  This article will clear your concept on how to Concatenate Arrays in Numpy. You can also do a similar operation using the NumPy append method.  I have tried my best to keep this article as simple as possible. In case you need to understand more, Please go for its official NumPy documentation.

Data Science Learner’s Team is continuously trying to simplify all aspect of Data Science for you. So keep reading.

Thanks

Data Science Learner Team . 

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner