Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Python Numpy concatenate

by suresh

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

The basic syntax of the Python Numpy concatenate function is as shown below

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

This is an another 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 concatenate function without axis parameter. This time, we use this parameter value while concatenating two dimensional arrays. Remember, If axis = 0 then the items in array b is vertically appended to a. Where as 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 like we shown below.

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 same size of arrays (joining 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

Th 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

Trending Posts

SQL SELECT Statement

Save R ggplot using ggsave

How to Enter Data into Power BI

Creating SSRS Matrix Report using Report Wizard

Tableau Show Me

MySQL Alias

SQL HOST_ID

SSIS FTP TASK Delete Remote Directory

Program to print Pascal Triangle in C

Aggregate Transformation in SSIS Advanced Mode

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

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

Home | About Us | Contact Us | Privacy Policy