Joining
Another equally important operation in array manipulation is joining arrays. Joining arrays involves combining multiple arrays into a single array that includes all the elements from each of the original arrays.
We perform this concatenation along the specified axes:
- if
axis = 0
(which is the default value), this implies concatenating the arrays by rows; - if
axis = 1
, this means concatenating the arrays by columns.
Join two arrays:
12345678import numpy as np array_1 = np.array([54, 6, 23, 1, 6]) array_2 = np.array([12, 67, 94, 2, 8 ]) array = np.concatenate((array_1, array_2)) print(array)
Concatenate two 2-D arrays along columns (axis=1
):
12345678import numpy as np array_1 = np.array([[0, 1, 2], [3, 4, 5]]) array_2 = np.array([[6, 7, 8], [9, 10, 11]]) array = np.concatenate((array_1, array_2), axis=1) print(array)
Concatenate two 2-D arrays along rows (default axis=0
):
12345678import numpy as np array_1 = np.array([[0, 1, 2], [3, 4, 5]]) array_2 = np.array([[6, 7, 8], [9, 10, 11]]) array = np.concatenate((array_1, array_2)) print(array)
Swipe to start coding
You have two arrays:
-
[[12, 56, 78], [35, 1, 5]]
; -
[[ 8, 65, 3], [ 1, 2, 3]]
.
You have to create the following combined array:
[[12 56 78 8 65 3]
[35 1 5 1 2 3]]
Lösung
Danke für Ihr Feedback!
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 4.76
Joining
Swipe um das Menü anzuzeigen
Another equally important operation in array manipulation is joining arrays. Joining arrays involves combining multiple arrays into a single array that includes all the elements from each of the original arrays.
We perform this concatenation along the specified axes:
- if
axis = 0
(which is the default value), this implies concatenating the arrays by rows; - if
axis = 1
, this means concatenating the arrays by columns.
Join two arrays:
12345678import numpy as np array_1 = np.array([54, 6, 23, 1, 6]) array_2 = np.array([12, 67, 94, 2, 8 ]) array = np.concatenate((array_1, array_2)) print(array)
Concatenate two 2-D arrays along columns (axis=1
):
12345678import numpy as np array_1 = np.array([[0, 1, 2], [3, 4, 5]]) array_2 = np.array([[6, 7, 8], [9, 10, 11]]) array = np.concatenate((array_1, array_2), axis=1) print(array)
Concatenate two 2-D arrays along rows (default axis=0
):
12345678import numpy as np array_1 = np.array([[0, 1, 2], [3, 4, 5]]) array_2 = np.array([[6, 7, 8], [9, 10, 11]]) array = np.concatenate((array_1, array_2)) print(array)
Swipe to start coding
You have two arrays:
-
[[12, 56, 78], [35, 1, 5]]
; -
[[ 8, 65, 3], [ 1, 2, 3]]
.
You have to create the following combined array:
[[12 56 78 8 65 3]
[35 1 5 1 2 3]]
Lösung
Danke für Ihr Feedback!
Awesome!
Completion rate improved to 4.76single