Python NumPy concatenate + 9 Examples

In this Python NumPy tutorial, we will discuss Python NumPy concatenate and also cover the below examples:

  • Python NumPy concatenate arrays
  • Python NumPy concatenate 1d arrays
  • Python NumPy concatenate 2d arrays
  • Python NumPy concatenate 2 arrays
  • Python NumPy concatenate multiple arrays
  • Python NumPy concatenate empty array
  • Python NumPy concatenate 3d arrays
  • Python NumPy concatenate vs append
  • Python NumPy concatenate float

Python NumPy concatenate

  • In this section, we will learn about python NumPy concatenate.
  • Concatenate means join a sequence of arrays along an existing axis.
  • In NumPy concatenate we can easily use the function np.concatenate().
  • This function is used to join two or more given NumPy arrays along the existing axis.
  • We need to use NumPy.concatenate method which contains three parameters first one is arrays.
  • Here you need to mention the arrays which you want to join or concatenate.
  • But while taking arrays you should remember that you can’t concatenate or join arrays of different dimensions
  • The dimension of the array should be the same.

Syntax:

Here is the syntax of numpy concatenate

numpy.concatenate
                 (
                  arrays,
                  axis=0,
                  out=None
                 )
  1. Arrays: The arrays must have the same shape, except in the dimension corresponding to the axis. (the first, by default).
  2. Axis: Along which axis you want to join NumPy arrays and by default value is 0 there is nothing but the first axis. So in one dimension, it contains only one dimension based on that axis you can join the NumPy arrays. when you take the axis as none it will flatten the given NumPy arrays first then it will concatenate that arrays.
  3. Out: The default value is none if provided this is the destination to place the result you can store the join arrays in another array or in any other destination.

Example:

import numpy as np

arr1 = np.array([[4, 6], [8, 9]])
arr2 = np.array([[3, 2]])
conc_arr = np.concatenate((arr1, arr2), axis=0)
print(conc_arr)

Here is the Screenshot of following given code

Python numpy concatenate
Python numpy concatenate

This is how NumPy concatenate works.

Read: Python NumPy Random + Examples

Python NumPy concatenate arrays

  • In this section, we will learn about Python NumPy concatenate arrays.
  • In numpy concatenate arrays we can easily use the function np.concatenate().
  • It 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 which means axis=0.
  • First, we create an array from the arranging function. So if I want to join the two given arrays then I need to mention the np. concatenate function.
  • When you take the axis as none it will flatten the given NumPy arrays first then it will concatenate that arrays.

Syntax:

Here is the syntax of numpy concatenate arrays

numpy.concatenate
                 (
                  arrays,
                  axis=0,
                  out=None
                 )

Example:

import numpy as np

arr1 = np.arange(8)
arr2 = np.arange(4)
conc_arr = np.concatenate((arr1, arr2), axis=0)
print(conc_arr)

Here is the Screenshot of following given code

Python numpy concatenate arrays
Python numpy concatenate arrays

The above code we can use to concatenate arrays in Python NumPy.

Read Python NumPy nan

Python NumPy concatenate 1d arrays

  • In this section, we will learn about python numpy concatenate 1d arrays.
  • One dimensional array means the collection of homogenous data in a single row. It is also known as a vector. When it is a single row or 1D array you have to use only one square bracket.
  • In numpy concatenate 1d arrays we can easily use the function np.concatenate().
  • In this method take two 1 dimensional arrays and concatenate them as an array sequence.
  • So you have to pass arrays inside the concatenate function because concatenate function is used to join a sequence of arrays.
  • We can also use the function numpy. stack() to join a sequence of arrays.

Syntax:

Here is the syntax of numpy concatenate 1d arrays

numpy.concatenate
                 (
                  arrays,
                  axis=0,
                  out=None
                 )

Example:

import numpy as np

arr1 = np.arange(6)
arr2 = np.arange(2)
conc_1darr = np.concatenate((arr1, arr2), axis=0) #one dimensional array
print(conc_1darr)

a = np.array([5, 4,2])
b = np.array([8, 1,3])
c = np.stack([a,b]).reshape(-1) # stack method for 1d array
print(c)

Here is the Screenshot of following given code

Python numpy concatenate 1d arrays
Python numpy concatenate 1d arrays

This is how to concatenate 1d arrays in Python NumPy.

Read: Check if NumPy Array is Empty in Python

Python NumPy concatenate 2d arrays

  • In this section, we will learn about python numpy concatenate 2d arrays.
  • A two-dimensional array means the collection of homogenous data in lists of a list. It is also known as a matrix. In a 2D array, you have to use two square brackets that is why it said lists of lists.
  • In two dimensions it contains two axiss based on the axis you can join the numpy arrays.
  • In numpy concatenate 2d arrays we can easily use the function np.concatenate().
  • In this method, the axis value is 1 to join the column-wise elements.

Syntax:

Here is the syntax of numpy concatenate 2d array

numpy.concatenate
                 (
                  arrays,
                  axis=1,
                  out=None
                 )

Example:

import numpy as np

arr1 = np.array([[6,5,3],[3,2,1]])
arr2 = np.array([[4,5,5],[4,3,5]])
conc_2darr = np.concatenate((arr1, arr2), axis=1) #TWO dimensional array
print(conc_2darr)

Here is the Screenshot of following given code

Python numpy concatenate 2d arrays
Python NumPy concatenate 2d arrays

This is how to concatenate 2d arrays using Python NumPy.

Read Python NumPy shape with examples

Python NumPy concatenate 2 arrays

  • In this section, we will learn about python NumPy concatenate 2 arrays.
  • We can join two arrays by using the function np. concatenate.
  • To concatenate two arrays either row-wise or column-wise we can use the axis parameter.
  • We can also use another method Vstack it means vertical stack. In this case, elements will be added in the terms of vertical.
  • In this method, the axis value is 1 to join the column-wise elements.

Syntax:

Here is the syntax of numpy concatenate 2 arrays

numpy.concatenate
                 (
                  arrays,
                  axis=1,
                  out=None
                 )

Example:

import numpy as np

arr1 = np.array([[3,4,6],[7,2,1]])
arr2 = np.array([[4,5,5],[4,3,5]])
conc_arr = np.concatenate((arr1, arr2), axis=1) 
print(conc_arr)

Here is the Screenshot of following given code

Python numpy concatenate 2 arrays
Python numpy concatenate 2 arrays

This is how to concatenate 2 arrays in Python NumPy.

Read: Python NumPy zeros

Python NumPy concatenate multiple arrays

  • In this section, we will learn about python numpy concatenate multiple arrays.
  • In multiple arrays, we can easily use the method np. concatenate().
  • This function essentially combines a NumPy array.
  • This function can operate both vertically and horizontally which means we can concatenate arrays together horizontally or vertically.

Syntax:

Here is the syntax of numpy concatenate multiple arrays

numpy.concatenate
                 (
                  arrays,
                  axis=1,
                  out=None
                 )

Example:

import numpy as np

arr1 = np.array([[3,4,6],[7,2,1]])
arr2 = np.array([[4,5,5],[4,3,5]])
arr3 = np.array([[6,5,7],[4,3,5]])
arr4 = np.array([[4,8,9],[1,3,4]])
conc_arr = np.concatenate((arr1, arr2,arr3,arr4), axis=0) 
print(conc_arr)

Here is the Screenshot of following given code

Python numpy concatenate multiple arrays
Python numpy concatenate multiple arrays

In the above example, we saw how to concatenate multiple arrays using Python NumPy.

Python NumPy concatenate empty array

  • In this section, we will learn about python NumPy concatenate empty arrays.
  • In this method, we can easily use the function np.stack().
  • Vstack() function is used to stack arrays in sequence vertically row-wise
  • In an empty array, it will return the output in the form of ndarray.
  • Empty has nothing to do with creating an array that is empty in the sense of having no elements.

Syntax:

Here is the syntax of numpy concatenate empty arrays

numpy.vstack
            (
            arrays,
            axis=0,
            out=None
            )

Example:

import numpy as np

arr1 = np.array([[5,7,8,5,4],[1,2,3,4,5]])
arr2 = np.array([], dtype=np.int64).reshape(0,5)
b = np.vstack([arr2, arr1])
print(b)

Here is the Screenshot of following given code

Python numpy concatenate empty array
Python numpy concatenate empty array

This is how to concatenate empty array in Python NumPy.

Read: Python NumPy Sum + Examples

Python NumPy concatenate 3d arrays

  • In this section, we will learn about Python NumPy concatenate 3d arrays.
  • In this method, we can easily use np.concatenate() function.
  • A 3-dimensional array is composed of 3 nested levels of arrays one for each dimension
  • In this method, the axis value is 0 and 1 to join the column and row-wise elements.

Syntax:

numpy.concatenate
                 (
                  arrays,
                  axis=1,
                  out=None
                 )

Example:

import numpy as np

arr1 = np.array([[4,5], [3, 3]])
arr2 = np.array([[3,4]])
conc_arr = np.concatenate((arr1, arr2), axis=0)
print(conc_arr)
conc2_arr = np.concatenate((arr1,arr2.T), axis=1)
print(conc2_arr)

Here is the Screenshot of following given code

Python numpy concatenate 3d arrays
Python numpy concatenate 3d arrays

This is how to concatenate 3d arrays using Python NumPy.

Python NumPy concatenation vs append

  • In this section, we will learn about NumPy concatenation vs append.
  • The Numpy append function allows us to add new values to the end of an existing NumPy array.
  • This function returns a copy of the existing array with the values appended to the specified axis.
  • In Concatenation It can be used to concatenate two arrays either row-wise or column-wise.
  • In this method we can use both the method np.append() and np.concatenate().
  • The append method will add an item to the end of an array and the Concatenation function will allow us to add two arrays together.
  • In concatenate function the input can be any dimension while in the append function all input must be of the same dimension.
  • In Concatenate the output array will be n-dimensional while in the case of append function the output will be (n+1) dimension array.

Syntax:

Here is the Syntax of concatenate function

numpy.concatenate
                 (
                  arrays,
                  axis=1,
                  out=None
                 )

Here is the Syntax of append function

numpy.append
            (
             arrays,
             axis=0,
             out=None
            )

Example:

import numpy as np

arr1 = np.array([[4,5], [3, 3]])
arr2 = np.array([[3,4]])
conc_arr = np.concatenate((arr1, arr2), axis=0)
print(conc_arr)


a = np.array([[1,2,3],[2,6,7]])
b = np.append(a,[6,7,8])
print(b)

Here is the Screenshot of following given code

Python numpy concatenate vs append
Python numpy concatenate vs append

Read: Python NumPy arange + Examples

Python NumPy concatenate float

  • In this section, we will learn about NumPy concatenate float.
  • In NumPy concatenate the default value of the dtype argument in the function header is None.
  • If you want to manually specify the data type, you can use the dtype parameter.
  • In this case, when we set dtype = float, the concatenate function produces an nd. array objects.

Syntax:

Here is the Syntax of concatenate float function

numpy.concatenate
                 (
                  arrays,
                  axis=1,
                  dtype=float
                  out=None
                 )

Example:

import numpy as np

arr1 = np.array([[4,5], [3, 3]])
arr2 = np.array([[3,4]])
conc_arr = np.concatenate((arr1, arr2), dtype ="float")
print(conc_arr)

Here is the Screenshot of following given code

Python numpy concatenate float
Python numpy concatenate float

This is how to concatenate float using Python NumPy.

You may also like the following Python tutorials:

In this python tutorial, we will discuss Python numpy concatenate and also cover the below examples:

  • Python numpy concatenate arrays
  • Python numpy concatenate 1d arrays
  • Python numpy concatenate 2d arrays
  • Python numpy concatenate 2 arrays
  • Python numpy concatenate multiple arrays
  • Python numpy concatenate empty array
  • Python numpy concatenate 3d arrays
  • Python numpy concatenate vs append
  • Python numpy concatenate float