Conteúdo do Curso
Ultimate NumPy
Ultimate NumPy
Array Concatenation
Array concatenation is a fundamental operation in NumPy that allows combining arrays along a specified axis to create larger, more comprehensive datasets. Essentially, concatenation involves joining arrays together to form a new array.
NumPy has a concatenate()
function that enables you to concatenate arrays along a specified axis:
axis=0
(the default value) concatenates the arrays by rows;axis=1
concatenates the arrays by columns.
The first parameter of this function is the sequence of arrays (a tuple
or list
of arrays) to concatenate, while axis
is the second parameter.
Here is an example with 1D arrays:
import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Concatenating 1D arrays along their only axis 0 concatenated_array = np.concatenate((array1, array2)) print(concatenated_array)
As you can see, with 1D arrays, everything is quite simple. Concatenation creates a 1D array with the elements of the first array followed by the elements of the second array.
Now let’s concatenate 2D arrays:
import numpy as np array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Concatenating along the axis 0 (rows) concatenated_array_rows = np.concatenate((array1, array2)) print(f'Axis = 0:\n{concatenated_array_rows}') # Concatenating along the axis 1 (columns) concatenated_array_columns = np.concatenate((array1, array2), axis=1) print(f'Axis = 1:\n{concatenated_array_columns}')
Here is the visualization:
The purple elements correspond to array1
, and the green ones to array2
.
In fact, we can concatenate any number of arrays, and it will work the same way.
Tarefa
Your task is to concatenate the sales data for both products by columns:
- Use the correct function for concatenation.
- Use
sales_data_2021
andsales_data_2022
in this order for concatenation. - Specify the second keyword argument correctly to concatenate by columns.
Obrigado pelo seu feedback!
Array Concatenation
Array concatenation is a fundamental operation in NumPy that allows combining arrays along a specified axis to create larger, more comprehensive datasets. Essentially, concatenation involves joining arrays together to form a new array.
NumPy has a concatenate()
function that enables you to concatenate arrays along a specified axis:
axis=0
(the default value) concatenates the arrays by rows;axis=1
concatenates the arrays by columns.
The first parameter of this function is the sequence of arrays (a tuple
or list
of arrays) to concatenate, while axis
is the second parameter.
Here is an example with 1D arrays:
import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Concatenating 1D arrays along their only axis 0 concatenated_array = np.concatenate((array1, array2)) print(concatenated_array)
As you can see, with 1D arrays, everything is quite simple. Concatenation creates a 1D array with the elements of the first array followed by the elements of the second array.
Now let’s concatenate 2D arrays:
import numpy as np array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Concatenating along the axis 0 (rows) concatenated_array_rows = np.concatenate((array1, array2)) print(f'Axis = 0:\n{concatenated_array_rows}') # Concatenating along the axis 1 (columns) concatenated_array_columns = np.concatenate((array1, array2), axis=1) print(f'Axis = 1:\n{concatenated_array_columns}')
Here is the visualization:
The purple elements correspond to array1
, and the green ones to array2
.
In fact, we can concatenate any number of arrays, and it will work the same way.
Tarefa
Your task is to concatenate the sales data for both products by columns:
- Use the correct function for concatenation.
- Use
sales_data_2021
andsales_data_2022
in this order for concatenation. - Specify the second keyword argument correctly to concatenate by columns.
Obrigado pelo seu feedback!
Array Concatenation
Array concatenation is a fundamental operation in NumPy that allows combining arrays along a specified axis to create larger, more comprehensive datasets. Essentially, concatenation involves joining arrays together to form a new array.
NumPy has a concatenate()
function that enables you to concatenate arrays along a specified axis:
axis=0
(the default value) concatenates the arrays by rows;axis=1
concatenates the arrays by columns.
The first parameter of this function is the sequence of arrays (a tuple
or list
of arrays) to concatenate, while axis
is the second parameter.
Here is an example with 1D arrays:
import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Concatenating 1D arrays along their only axis 0 concatenated_array = np.concatenate((array1, array2)) print(concatenated_array)
As you can see, with 1D arrays, everything is quite simple. Concatenation creates a 1D array with the elements of the first array followed by the elements of the second array.
Now let’s concatenate 2D arrays:
import numpy as np array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Concatenating along the axis 0 (rows) concatenated_array_rows = np.concatenate((array1, array2)) print(f'Axis = 0:\n{concatenated_array_rows}') # Concatenating along the axis 1 (columns) concatenated_array_columns = np.concatenate((array1, array2), axis=1) print(f'Axis = 1:\n{concatenated_array_columns}')
Here is the visualization:
The purple elements correspond to array1
, and the green ones to array2
.
In fact, we can concatenate any number of arrays, and it will work the same way.
Tarefa
Your task is to concatenate the sales data for both products by columns:
- Use the correct function for concatenation.
- Use
sales_data_2021
andsales_data_2022
in this order for concatenation. - Specify the second keyword argument correctly to concatenate by columns.
Obrigado pelo seu feedback!
Array concatenation is a fundamental operation in NumPy that allows combining arrays along a specified axis to create larger, more comprehensive datasets. Essentially, concatenation involves joining arrays together to form a new array.
NumPy has a concatenate()
function that enables you to concatenate arrays along a specified axis:
axis=0
(the default value) concatenates the arrays by rows;axis=1
concatenates the arrays by columns.
The first parameter of this function is the sequence of arrays (a tuple
or list
of arrays) to concatenate, while axis
is the second parameter.
Here is an example with 1D arrays:
import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Concatenating 1D arrays along their only axis 0 concatenated_array = np.concatenate((array1, array2)) print(concatenated_array)
As you can see, with 1D arrays, everything is quite simple. Concatenation creates a 1D array with the elements of the first array followed by the elements of the second array.
Now let’s concatenate 2D arrays:
import numpy as np array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Concatenating along the axis 0 (rows) concatenated_array_rows = np.concatenate((array1, array2)) print(f'Axis = 0:\n{concatenated_array_rows}') # Concatenating along the axis 1 (columns) concatenated_array_columns = np.concatenate((array1, array2), axis=1) print(f'Axis = 1:\n{concatenated_array_columns}')
Here is the visualization:
The purple elements correspond to array1
, and the green ones to array2
.
In fact, we can concatenate any number of arrays, and it will work the same way.
Tarefa
Your task is to concatenate the sales data for both products by columns:
- Use the correct function for concatenation.
- Use
sales_data_2021
andsales_data_2022
in this order for concatenation. - Specify the second keyword argument correctly to concatenate by columns.