How to implement Array merging with numpy
Editor to share with you how numpy to achieve array merging, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
First create two new arrays for merging
Import numpy as nparr1 = np.array ([[1,2,3], [4,5,6]]) print (arr1)
Result:
[[1 2 3]
[4 5 6]]
Arr2 = np.array ([[7,8,9], [10,11,12]]) print (arr2)
Result:
[[7 8 9]
[10 11 12]]
1. Horizontal merger
Horizontal merging is a simple splicing of two arrays with an equal number of rows in the row direction. Unlike DataFrame merge, numpy number combination does not need common columns, but simply splices two arrays together. There are three methods: concatenate, hstack and column_stack.
1.1 concatenate method
The concatenate method passes the two arrays to be merged to concatenate as a list and indicates whether to merge in the row direction or column direction by setting the axis parameter. The parameter axis=1 indicates that the array is merged in the row direction
Print (np.concatenate ([arr1, arr2], axis=1))
Result:
[[1 2 3 7 8 9]
[4 5 6 10 11 12]]
1.2 hstack method
In the hstack method, the horizontal merging of arrays can be achieved by passing two arrays to hstack in the form of tuples.
Print (np.hstack ((arr1, arr2)
Result:
[[1 2 3 7 8 9]
[4 5 6 10 11 12]]
1.3 column_stack method
The column_stack method is basically the same as the hstack method, and the horizontal merging of the array can be achieved by passing two arrays to be merged to column_stack in the form of tuples.
Print (np.column_stack ((arr1, arr2)
Result:
[[1 2 3 7 8 9]
[4 5 6 10 11 12]]
two。 Vertical merger
Vertical merging is to concatenate two arrays with equal columns in the column direction, which can be realized by concatenate, vstack and row_stack.
2.1 concatenate method
The concatenate method passes the two arrays to be merged to concatenate as a list and indicates whether to merge in the row direction or column direction by setting the axis parameter. The parameter axis=0 indicates that the array is merged in the column direction
Print (np.concatenate ([arr1, arr2], axis=0))
Result:
[[1 2 3]
[4 5 6]
[7 8 9]
[10 11 12]]
2.2 vstack method
The vstack method is corresponding to the hstack method. Similarly, as long as the two arrays to be merged are passed to vstack in the form of tuples, the vertical merging of the arrays can be achieved.
Print (np.vstack ((arr1, arr2)
Result:
[[1 2 3]
[4 5 6]
[7 8 9]
[10 11 12]]
2.3 row_stack method
The row_stack method is corresponding to the column_stack method. Similarly, as long as the two arrays to be merged are passed to row_stack in the form of tuples, the vertical merging of the arrays can be achieved.
Print (np.row_stack ((arr1, arr2)
Result:
[[1 2 3]
[4 5 6]
[7 8 9]
[10 11 12]]
These are all the contents of the article "how to merge arrays in numpy". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!