Projects/array_copying.py

19 lines
1.1 KiB
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([360, 420]) # Modify 1st element, Q3 & Q4
sales_data_2022[1, :2] = np.array([280, 330]) # Modify 2nd element, Q1 & Q2
print(f'Quarterly sales in 2021:\n{sales_data_2021}')
print(f'Quarterly revised sales in 2022:\n{sales_data_2022}')