18 lines
985 B
Python
18 lines
985 B
Python
import numpy as np
|
|
"""
|
|
This script demonstrates how to copy and modify NumPy arrays for simulated sales data.
|
|
|
|
- Imports NumPy for array manipulation.
|
|
- Defines `sales_data_2021` as a 2D NumPy array representing quarterly sales for two products in 2021.
|
|
- Creates a deep copy of `sales_data_2021` named `sales_data_2022` to simulate sales for the next year.
|
|
- Updates the last two quarters' sales for the first product in `sales_data_2022` with new values (390 and 370).
|
|
- Prints both the original and modified sales data arrays for comparison.
|
|
"""
|
|
# Simulated quarterly sales data for two products in 2021
|
|
sales_data_2021 = np.array([[350, 420, 380, 410], [270, 320, 290, 310]])
|
|
# Create a copy of sales_data_2021
|
|
sales_data_2022 = np.copy(sales_data_2021)
|
|
# Assign the NumPy array with elements 390 and 370 to the correct slice
|
|
sales_data_2022[0, -2:] = np.array([390, 370])
|
|
print(f'Quarterly sales in 2021:\n{sales_data_2021}')
|
|
print(f'Quarterly sales in 2022:\n{sales_data_2022}') |