This is the 27th day of my participation in the August More Text Challenge

Start by creating two new arrays for merge

import numpy as np
​
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr1)
Copy the code

result:

[[1 2 3]
 [4 5 6]]
Copy the code
arr2 = np.array([[7, 8, 9], [10, 11, 12]])
print(arr2)
Copy the code

result:

[[7 8 9] [10 11 12]]Copy the code

1. Horizontal merger

A horizontal merge is a simple concatenation of two arrays with the same number of rows. Unlike DataFrame merges, NUMpy number combinations do not require a common column, but simply concatenate two arrays together using concatenate, hstack, or column_stack

1.1 concatenate method

The concatenate method passes the two arrays to be merged as a list and sets the Axis parameter to indicate whether to merge in row or column directions. The axis=1 parameter indicates the merge in the array row direction

print(np.concatenate([arr1, arr2], axis=1))
Copy the code

result:

[12 3 7 8 9] [4 5 6 10 11 12]Copy the code

1.2 hstack method

In the hSTACK method, the two arrays to be merged are passed to the Hstack as tuples to achieve the purpose of horizontal array merging

print(np.hstack((arr1, arr2)))
Copy the code

result:

[12 3 7 8 9] [4 5 6 10 11 12]Copy the code

1.3 column_stack method

The column_stack method is basically the same as the HSTACK method. The two arrays to be merged are passed to the column_stack as tuples to achieve horizontal array merging

print(np.column_stack((arr1, arr2)))
Copy the code

result:

[12 3 7 8 9] [4 5 6 10 11 12]Copy the code

2. Vertical mergers

Vertical merge is the concatenate, vstack, and row_stack methods for concatenate, vstack, and row_stack

2.1 concatenate method

The concatenate method passes the two arrays to be merged as a list and sets the Axis parameter to indicate whether to merge in row or column directions. The axis=0 argument represents merging arrays in column directions

print(np.concatenate([arr1, arr2], axis=0))
Copy the code

result:

[1 2 3] [4 5 6] [7 8 9] [10 11 12]Copy the code

2.2 vstack method

The vSTACK method is the counterpart of the HSTACK method, and the vertical array combination can be achieved by passing two arrays to be merged to the Vstack in the form of tuples

print(np.vstack((arr1, arr2)))
Copy the code

result:

[1 2 3] [4 5 6] [7 8 9] [10 11 12]Copy the code

2.3 row_stack method

The row_stack method is the counterpart of the column_stack method, and the vertical array combination can be achieved by passing the two arrays to be merged as tuples to row_stack

print(np.row_stack((arr1, arr2)))
Copy the code

result:

[1 2 3] [4 5 6] [7 8 9] [10 11 12]Copy the code