Skip to Main Content

Python for Basic Data Analysis

Start your data science journey with Python. Learn practical Python programming skills for 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.

Video Guide

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]]