Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python Numpy concatenate

by suresh

The Python Numpy concatenate function used to Join two or more arrays together. And it returns a concatenated ndarray as an output.

The syntax of the Python Numpy concatenate function is

numpy.concatenate((array1, array2,....), axis = 0)
  • array1, array2,… are the arrays that you want to concatenate. The arrays that you pass to this concatenate function must have the same shape. However, you can choose the arrays with different dimensions.
  • axis – This is an optional argument with default value as 0. Use this to specify in which way (horizontal or Vertical) concatenation should be done.

Python Numpy concatenate

In this example, we declared two numpy ndarrays. Next, we used this Python Numpy concatenate function to join those two arrays.

import numpy as np
 
a = np.array([1, 2, 3])
print(a)
 
b = np.array([4, 5, 6])
print(b)
 
print('\n---Numpy concatenation---')
print(np.concatenate((a, b)))

OUTPUT

Python Numpy concatenate 1

The Numpy concatenate function is not limited to join two arrays. You can use this function to concatenate more than two. Here, we are joining four different arrays using this function.

import numpy as np
a = np.array([1, 2, 3])
print(a)
 
b = np.array([4, 5, 6])
print(b)
 
c = np.array([7, 8, 9])
print(c)
 
d = np.array([10, 11, 12])
print(d)
 
print('\n---Numpy concatenation---')
print(np.concatenate((a, b, c, d)))

OUTPUT

Python Numpy concatenate 2

Python Numpy concatenate 2D array

In this example, we are using Numpy concatenate function to join two dimensional arrays.

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print()
 
b = np.array([[7, 8, 9],[10, 11, 12]])
print(b)
 
print('\n---Numpy concatenation---')
print(np.concatenate((a, b)))

OUTPUT

Python Numpy concatenate 2D array 1

It is another Python Numpy example to concatenate 2D arrays. 

import numpy as np
a = np.array([[10, 20, 30, 40]])
print(a)
 
b = np.array([[50 ,60, 70, 80], [90 ,100, 110, 120]])
print(b)
 
print('\n---Numpy concatenate Array---')
print(np.concatenate((a, b)))
 
print('\n---Numpy concatenate Array---')
print(np.concatenate((b, a)))

OUTPUT

Python Numpy concatenate 2D array 2

Python Numpy concatenate 2D array with axis

Until now, we are using a concatenate function without an axis parameter. This time, we use this parameter value while concatenating two-dimensional arrays. Remember, If axis = 0, then the items in array b vertically appended to a. Whereas axis = 1 horizontally appends array items in b to a.

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print()
 
b = np.array([[7, 8, 9],[10, 11, 12]])
print(b)
 
print('\n---Numpy concatenation of Two Dimensional Array---')
print(np.concatenate((a, b), axis = 0))
 
print('\n---Numpy concatenation of Two Dimensional Array---')
print(np.concatenate((a, b), axis = 1))

OUTPUT

Python Numpy concatenate 2D array axis 1

In general, you don’t have to specify the axis. I mean, you can directly use the value.

import numpy as np
a = np.array([[10, 20, 30], [40, 50, 60]])
print(a)
print()
 
b = np.array([[70, 80, 90],[100, 110, 120]])
print(b)
 
print('\n---Numpy concatenation of Two Dimensional Array---')
print(np.concatenate((a, b), 0))
 
print('\n---Numpy concatenation of Two Dimensional Array---')
print(np.concatenate((a, b), 1))

OUTPUT

Python Numpy concatenate 2D array axis 2

Python Numpy concatenate 3D array

In this example, we are using Numpy concatenate function on three-dimensional arrays. First, we created two 3D random arrays using randint. Next, we used the concatenate function with different axis values.

import numpy as np
a = np.array(np.random.randint(0, 10, size = (2, 3, 4)))
print(a)
print()
 
b = np.array(np.random.randint(11, 20, size = (2, 3, 4)))
print(b)
 
print('\n---Numpy concatenation of Three Dimensional Array---')
print(np.concatenate((a, b), axis = -1))

OUTPUT

Python Numpy concatenate 3D array 1
print('\n---Numpy concatenation of Three Dimensional Array---')
print(np.concatenate((a, b), axis = -2))

OUTPUT

Python Numpy concatenate 3D array 2

Python Numpy concatenate Different sizes

Until now, we are working with the same size of arrays (joining the same size arrays). Let me join different size arrays using this concatenate function.

import numpy as np
x = np.array([1, 2, 3])
print(x)
 
y = np.array([4, 5])
print(y)
 
print('\n---Numpy concatenation---')
print(np.concatenate((x, y)))
print()
 
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print()
 
b = np.array([[7, 8, 9],[10, 11, 12], [13, 14, 15]])
print(b)
 
print('\n---Numpy concatenation of two Dimensional Array---')
print(np.concatenate((a, b), axis = 0))
 
print('\n---Numpy concatenation of two Dimensional Array---')
print(np.concatenate((b, a.T), axis = 1))

OUTPUT

Python Numpy concatenate Different Dimensions

Python Numpy hstack

The Python Numpy hstack function horizontally appends the array items, which is similar to axis = 1.

import numpy as np
a = np.array([10, 20, 30, 40])
print(a)
 
b = np.array([50 ,60, 70, 80])
print(b)
 
print('\n---Numpy hstack on one Dimensional Array---')
print(np.hstack((a, b)))

OUTPUT

Python Numpy hstack concatenate 1

Let me use this Numpy hstack function to concatenate two-dimensional arrays.

import numpy as np
a = np.array(np.random.randint(0, 10, size = (3, 3)))
print(a)
print()
 
b = np.array(np.random.randint(11, 20, size = (3, 3)))
print(b)
 
print('\n---Numpy hstack on Two Dimensional Array---')
print(np.hstack((a, b)))

OUTPUT

Python Numpy hstack concatenate 2

Python Numpy vstack

The Python Numpy vstack function vertically appends the array items, which is similar to axis = 0.

import numpy as np
a = np.array([10, 20, 30, 40])
print(a)
 
b = np.array([50 ,60, 70, 80])
print(b)
 
print('\n---Numpy vstack on one Dimensional Array---')
print(np.vstack((a, b)))

OUTPUT

Python Numpy vstack concatenate 1

In this example, we are using Numpy vstack function to concatenate two-dimensional arrays.

import numpy as np
a = np.array(np.random.randint(0, 10, size = (3, 3)))
print(a)
print()
 
b = np.array(np.random.randint(11, 20, size = (3, 3)))
print(b)
 
print('\n---Numpy vstack on Two Dimensional Array---')
print(np.vstack((a, b)))

OUTPUT

Python Numpy vstack concatenate 2

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy