Skip to Main Content
It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.

Python for Basic Data Analysis: NP.7 Combining 2 arrays

Get started on your learning journey towards data science using Python. Equip yourself with practical skills in Python programming for the purpose of basic data manipulation and analysis.

Combining 2 arrays

Combining 2 concatenate

In NumPy, you can join 2 or more arrays together using np.concatenate. To do so, you will need to ensure that if you are adding a row, the rows of both arrays must be the same. Likewise for columns.


Syntax

np.concatenate(array1, array2)


Once again we start by creating some arrays with .arange

Combining arrays with concatenate

Stack horizontally 

Arrays can be stacked horizontally - meaning that you can join the arrays by the sides. To do this, we need to ensure that the number of rows in all arrays that are to be joined together are the same.
 

Stack vertically

Arrays can be also be stacked vertically - meaning that you can join the arrays by the top / bottom. To do this, we need to ensure that the number of columns in all arrays that are to be joined together are the same.

Exercises

Rearrange the following arrays and stack them vertically with 5 columns:

myArray_1 = np.arange(20)
myArray_2 = np.arange(30)


Your results should look like this:

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]
 [25 26 27 28 29]]

Video Guide