Open In App

Joining NumPy Array

Last Updated : 01 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

NumPy provides various functions to combine arrays. In this article, we will discuss some of the major ones.

Method 1: Using numpy.concatenate()

The concatenate function in NumPy joins two or more arrays along a specified axis. 

Syntax:

numpy.concatenate((array1, array2, ...), axis=0)

The first argument is a tuple of arrays we intend to join and the second argument is the axis along which we need to join these arrays. Check out the following example showing the use of numpy.concatenate.

Python3




import numpy as np
  
array_1 = np.array([1, 2])
array_2 = np.array([3, 4])
  
array_new = np.concatenate((array_1, array_2))
print(array_new)


Output: 

[1 2 4 5]

By default, the value of the axis is set to 0. You can change it by specifying a value for the axis in the second argument. The following code joins two arrays along rows.

Python3




import numpy as np
  
array_1 = np.array([[1, 2], [3, 4]])
array_2 = np.array([[5, 6], [7, 8]])
  
array_new = np.concatenate((array_1, array_2), axis=1)
print(array_new)


Output:

[[1 2 5 6]
 [3 4 7 8]]

Method 2: Using numpy.stack()

The stack() function of NumPy joins two or more arrays along a new axis.

Syntax:

numpy.stack(arrays, axis=0)

The following code demonstrates the use of numpy.stack().

Python3




import numpy as np
  
array_1 = np.array([1, 2, 3, 4])
array_2 = np.array([5, 6, 7, 8])
  
array_new = np.stack((array_1, array_2), axis=1)
print(array_new)


Output:

[[1 5]
 [2 6]
 [3 7]
 [4 8]]

The arrays are joined along a new axis.

Method 3: numpy.block()

numpy.block is used to create nd-arrays from nested blocks of lists.

Syntax:

numpy.block(arrays)

The following example explains the working of numpy.block().

Python3




import numpy as np
  
block_1 = np.array([[1, 1], [1, 1]])
block_2 = np.array([[2, 2, 2], [2, 2, 2]])
block_3 = np.array([[3, 3], [3, 3], [3, 3]])
block_4 = np.array([[4, 4, 4], [4, 4, 4], [4, 4, 4]])
  
block_new = np.block([
    [block_1, block_2],
    [block_3, block_4]
])
  
print(block_new)


Output:

[[1 1 2 2 2]
 [1 1 2 2 2]
 [3 3 4 4 4]
 [3 3 4 4 4]
 [3 3 4 4 4]]

In this example, we assembled a block matrix( block_new ) from 4 separate 2-d arrays (block_1,block_2,block_3,block_4 ).



Previous Article
Next Article

Similar Reads

Python | Joining unicode list elements
Sometimes we can receive data in different formats other than the conventional list or strings or integers or characters. Python has many other data types and knowledge of handling them is usually useful. This article demonstrates the joining of Unicode characters in the list. Let's discuss certain ways in which this can be done. Method #1 : Using
4 min read
Python | Joining only adjacent words in list
Sometimes, more than one type of data can come in Python list and sometimes it's undesirably tokenized and hence we require to join the words that have been tokenized and leave the digits as they are. Let's discuss certain ways in which this task can be achieved. Method #1 : Using list comprehension + "*" operator This task can be performed using t
2 min read
Python | Merge list of tuple into list by joining the strings
Sometimes, we are required to convert list of tuples into a list by joining two element of tuple by a special character. This is usually with the cases with character to string conversion. This type of task is usually required in the development domain to merge the names into one element. Let’s discuss certain ways in which this can be performed. L
6 min read
Tableau - Joining data files with inconsistent labels
Designing good data has a fundamental principle of spreading the data out so that each data table stores the information about a particular business entity. Let's suppose that the model has a set of restaurants which are located in different cities. Each of these restaurants could have a restaurant ID, the restaurant city and the restaurant state.
2 min read
Joining Threads in Python
Like running multiple independent programs simultaneously, running multiple threads simultaneously also has similar features with some added benefits, which are as follows : Multiple threads share the same data space along with the main thread within a process. Hence, they can easily share information or can communicate with each other unlike if th
3 min read
Joining Excel Data from Multiple files using Python Pandas
Let us see how to join the data of two excel files and save the merged data as a new Excel file. We have 2 files, registration details.xlsx and exam results.xlsx. registration details.xlsx We are having 7 columns in this file with 14 unique students details. Column names are as follows : Admission Date Name of Student Gender DOB Student email Id En
2 min read
Joining two Pandas DataFrames using merge()
Let us see how to join two Pandas DataFrames using the merge() function. merge() Syntax : DataFrame.merge(parameters) Parameters : right : DataFrame or named Series how : {‘left’, ‘right’, ‘outer’, ‘inner’}, default ‘inner’ on : label or list left_on : label or list, or array-like right_on : label or list, or array-like left_index : bool, default F
2 min read
Joining Tables using MultiMaps
Joining two different tables on their matching columns can be done using nested loops, but a more efficient and scalable way is to use multimaps. The idea is to map from each column value that we want to join to all the rows that contain it, to generate a multimap from a table out of both tables. The multimap generated has to be hash-based. Hashing
3 min read
Prevent duplicated columns when joining two Pandas DataFrames
Column duplication usually occurs when the two data frames have columns with the same name and when the columns are not used in the JOIN statement. In this article, let us discuss the three different methods in which we can prevent duplication of columns when joining two data frames. Syntax: pandas.merge(left, right, how='inner', on=None, left_on=N
5 min read
NumPy Array Sorting | How to sort NumPy Array
Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.argsort() functionUsing np.lexsort() functionUsing s
4 min read
Practice Tags :