• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / NumPy / NumPy concatenate / How To Concatenate Arrays in NumPy?

How To Concatenate Arrays in NumPy?

April 2, 2018 by cmdlinetips

NumPy Concatenate
NumPy Concatenate Arrays

Often you may have two or more NumPY arrays and want to concatenate/join/merge them into a single array. Python offers multiple options to join/concatenate NumPy arrays.

Common operations include given two 2d-arrays, how can we concatenate them row wise or column wise. NumPy’s concatenate function allows you to concatenate two arrays either by rows or by columns. Let us see a couple of examples of NumPy’s concatenate function.

Let us first import the NumPy package.

# import numpy
import numpy as np

Let us create a NumPy array using arange function in NumPy. The 1d-array starts at 0 and ends at 8

array = np.arange(9)
array

We can use NumPy’s reshape function to convert the 1d-array to 2d-array of dimension 3×3, 3 rows and 3 columns. NumPy’s reshape function takes a tuple as input.

array2D_1 = array.reshape((3,3))
array2D_1

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

Let us create second 2d-array by using arange and reshape functions. The second 2d-array starts at 10 and ends at 18

>array2D_2 = np.arange(10,19).reshape(3,3)
>array2D_2

array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])

NumPy concatenate

NumPy’s concatenate function can be used to concatenate two arrays either row-wise or column-wise. Concatenate function can take two or more arrays of the same shape and by default it concatenates row-wise i.e. axis=0. The resulting array after row-wise concatenation is of the shape 6 x 3, i.e. 6 rows and 3 columns.

How To Concatenate 2 NumPy Arrays Row-wise?

# concatenate 2 numpy arrays: row-wise
>np.concatenate((array2D_1, array2D_2))

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])

How To Concatenate 2 NumPy Arrays Column-wise?

We can also concatenate 2 NumPy arrays by column-wise by specifying axis=1. Now the resulting array is a wide matrix with more columns than rows; in this example, 3 rows and 6 columns.

# concatenate 2 numpy arrays: column-wise
>np.concatenate((array2D_1,array2D_2),axis=1)
array([[ 0,  1,  2, 10, 11, 12],
       [ 3,  4,  5, 13, 14, 15],
       [ 6,  7,  8, 16, 17, 18]])

How To Concatenate more than 2 NumPy Arrays Row-wise?

NumPy’s concatenate function can be used with more than 2 arrays. Here is an example of concatenating 3 NumPy arrays row-wise. We specify the three arrays that we want to concatenate as a tuple.

# concatenate 3 numpy arrays: row-wise
>np.concatenate((array2D_1, array2D_2, array2D_1))
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18],
       [ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8]])

In addition to the concatenate function, NumPy also offers two convenient functions hstack and vstack to stack/combine arrays horizontally or vertically.

Both hstack and vstack, under the hood calls on concatenate with axis =1 and axis=0 options.

Here are the examples of using hstack and vstack.

NumPy vstack example

NumPy’s vstack stacks arrays in sequence vertically i.e. row wise. And the result is the same as using concatenate with axis=0.

>np.vstack((array2D_1, array2D_2))

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])

Another common use of Numpy’s hstack is to use it to combine two 1d-numpy arrays to one 2d-numpy array. For example, if we have two one-dimensional arrays,

x = np.ones(4)
y = np.arange(1,5)
print(x)
print(y)
[1. 1. 1. 1.]
[1 2 3 4]

We can use Numpy’s vstack to create 2d-array of size 2×4

print(np.vstack((x, y)))
[[1. 1. 1. 1.]
 [1. 2. 3. 4.]]

Similarly, with transpose we get 2d-array of 4×2 using vstack.

print(np.vstack((x, y)).T)
[[1. 1.]
 [1. 2.]
 [1. 3.]
 [1. 4.]]

NumPy hstack example

NumPy’s hstack stacks arrays horizontally i.e. column wise. And the result is the same as using concatenate with axis=1.

>np.hstack((array2D_1, array2D_2))

array([[ 0,  1,  2, 10, 11, 12],
       [ 3,  4,  5, 13, 14, 15],
       [ 6,  7,  8, 16, 17, 18]])

How to Concatenate Multiple 1d-Arrays?

NumPy’s concatenate function can also be used to concatenate more than two numpy arrays. Here is an example, where we have three 1d-numpy arrays and we concatenate the three arrays in to a single 1d-array.

Let use create three 1d-arrays in NumPy.

x = np.arange(1,3)
y = np.arange(3,5)
z= np.arange(5,7)

And we can use np.concatenate with the three numpy arrays in a list as argument to combine into a single 1d-array

>np.concatenate([x,y,z])

array([1, 2, 3, 4, 5, 6])

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object sophisticated (broadcasting) functions tools for integrating C/C++ and Fortran code useful linear algebra, Fourier transform, and random number capabilities10 Basic Arithmetic Operations with NumPy array Default Thumbnail3 Basic Commands to Manipulate NumPy 2d-arrays Default Thumbnail12 Basic Commands with NumPy Array Default ThumbnailHow To Concatenate Two or More Pandas DataFrames?

Filed Under: NumPy concatenate, NumPy hstack, NumPy vstack, Python Tips Tagged With: NumPy concatenate, NumPy hstack, NumPy vstack, Python Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version