Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Copying Arrays | Commonly used NumPy Functions
Ultimate NumPy
course content

Course Content

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

bookCopying Arrays

Often, you need to create a copy of an array to make changes without affecting the original array.

Simple Assignment

First, we'll discuss why we can't simply create another variable using array_2 = array_1, where array_1 is our original array. Let's look at an example:

123456
import numpy as np array_1 = np.array([1, 2, 3]) array_2 = array_1 # Setting the first element of array_2 to 10 array_2[0] = 10 print(array_1)
copy

We changed the value of the first element of array_2 to 10, but this assignment also changed the value of the first element of array_1 to 10.

Note

With array_2 = array_1, you are not creating a new array; instead, you are creating a reference to the same array in memory. Therefore, any changes made to array_2 will also affect array_1.

To solve this problem, we could write array_2 = np.array([1, 2, 3]), but that would mean writing the same code twice. Remember the key principle in coding: Don't repeat yourself.

ndarray.copy() Method

Luckily, NumPy has an ndarray.copy() method as a solution to this problem. Let's see it in action:

12345678
import numpy as np array_1 = np.array([1, 2, 3]) # Copying the contents of array_1 array_2 = array_1.copy() # Setting the first element of array_2 to 10 array_2[0] = 10 print(f'Initial array: {array_1}') print(f'Modified copy: {array_2}')
copy

Now, we have created a new array for array_2 with the same elements as array_1.

For 2D arrays, the copying procedure is exactly the same.

numpy.copy() Function

Instead of the .copy() method, we can also use the copy() function, which takes the array as its parameter: array_2 = np.copy(array_1).

Both the function and the method work the same; however, there is one nuance. They both have the order parameter, which specifies the memory layout of the array, but their default values are different. If you want to read more about it, here is the documentation: ndarray.copy, numpy.copy.

Task
test

Swipe to show code editor

  1. Create a copy of sales_data_2021 using the correct method of a NumPy array and store it in sales_data_2022.

  2. Assign the NumPy array with elements 390 and 370 to the last two elements of the first row (1D array) of sales_data_2022. Use a positive index and a slice with only negative start specified for indexing sales_data_2022.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
toggle bottom row

bookCopying Arrays

Often, you need to create a copy of an array to make changes without affecting the original array.

Simple Assignment

First, we'll discuss why we can't simply create another variable using array_2 = array_1, where array_1 is our original array. Let's look at an example:

123456
import numpy as np array_1 = np.array([1, 2, 3]) array_2 = array_1 # Setting the first element of array_2 to 10 array_2[0] = 10 print(array_1)
copy

We changed the value of the first element of array_2 to 10, but this assignment also changed the value of the first element of array_1 to 10.

Note

With array_2 = array_1, you are not creating a new array; instead, you are creating a reference to the same array in memory. Therefore, any changes made to array_2 will also affect array_1.

To solve this problem, we could write array_2 = np.array([1, 2, 3]), but that would mean writing the same code twice. Remember the key principle in coding: Don't repeat yourself.

ndarray.copy() Method

Luckily, NumPy has an ndarray.copy() method as a solution to this problem. Let's see it in action:

12345678
import numpy as np array_1 = np.array([1, 2, 3]) # Copying the contents of array_1 array_2 = array_1.copy() # Setting the first element of array_2 to 10 array_2[0] = 10 print(f'Initial array: {array_1}') print(f'Modified copy: {array_2}')
copy

Now, we have created a new array for array_2 with the same elements as array_1.

For 2D arrays, the copying procedure is exactly the same.

numpy.copy() Function

Instead of the .copy() method, we can also use the copy() function, which takes the array as its parameter: array_2 = np.copy(array_1).

Both the function and the method work the same; however, there is one nuance. They both have the order parameter, which specifies the memory layout of the array, but their default values are different. If you want to read more about it, here is the documentation: ndarray.copy, numpy.copy.

Task
test

Swipe to show code editor

  1. Create a copy of sales_data_2021 using the correct method of a NumPy array and store it in sales_data_2022.

  2. Assign the NumPy array with elements 390 and 370 to the last two elements of the first row (1D array) of sales_data_2022. Use a positive index and a slice with only negative start specified for indexing sales_data_2022.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
toggle bottom row

bookCopying Arrays

Often, you need to create a copy of an array to make changes without affecting the original array.

Simple Assignment

First, we'll discuss why we can't simply create another variable using array_2 = array_1, where array_1 is our original array. Let's look at an example:

123456
import numpy as np array_1 = np.array([1, 2, 3]) array_2 = array_1 # Setting the first element of array_2 to 10 array_2[0] = 10 print(array_1)
copy

We changed the value of the first element of array_2 to 10, but this assignment also changed the value of the first element of array_1 to 10.

Note

With array_2 = array_1, you are not creating a new array; instead, you are creating a reference to the same array in memory. Therefore, any changes made to array_2 will also affect array_1.

To solve this problem, we could write array_2 = np.array([1, 2, 3]), but that would mean writing the same code twice. Remember the key principle in coding: Don't repeat yourself.

ndarray.copy() Method

Luckily, NumPy has an ndarray.copy() method as a solution to this problem. Let's see it in action:

12345678
import numpy as np array_1 = np.array([1, 2, 3]) # Copying the contents of array_1 array_2 = array_1.copy() # Setting the first element of array_2 to 10 array_2[0] = 10 print(f'Initial array: {array_1}') print(f'Modified copy: {array_2}')
copy

Now, we have created a new array for array_2 with the same elements as array_1.

For 2D arrays, the copying procedure is exactly the same.

numpy.copy() Function

Instead of the .copy() method, we can also use the copy() function, which takes the array as its parameter: array_2 = np.copy(array_1).

Both the function and the method work the same; however, there is one nuance. They both have the order parameter, which specifies the memory layout of the array, but their default values are different. If you want to read more about it, here is the documentation: ndarray.copy, numpy.copy.

Task
test

Swipe to show code editor

  1. Create a copy of sales_data_2021 using the correct method of a NumPy array and store it in sales_data_2022.

  2. Assign the NumPy array with elements 390 and 370 to the last two elements of the first row (1D array) of sales_data_2022. Use a positive index and a slice with only negative start specified for indexing sales_data_2022.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Often, you need to create a copy of an array to make changes without affecting the original array.

Simple Assignment

First, we'll discuss why we can't simply create another variable using array_2 = array_1, where array_1 is our original array. Let's look at an example:

123456
import numpy as np array_1 = np.array([1, 2, 3]) array_2 = array_1 # Setting the first element of array_2 to 10 array_2[0] = 10 print(array_1)
copy

We changed the value of the first element of array_2 to 10, but this assignment also changed the value of the first element of array_1 to 10.

Note

With array_2 = array_1, you are not creating a new array; instead, you are creating a reference to the same array in memory. Therefore, any changes made to array_2 will also affect array_1.

To solve this problem, we could write array_2 = np.array([1, 2, 3]), but that would mean writing the same code twice. Remember the key principle in coding: Don't repeat yourself.

ndarray.copy() Method

Luckily, NumPy has an ndarray.copy() method as a solution to this problem. Let's see it in action:

12345678
import numpy as np array_1 = np.array([1, 2, 3]) # Copying the contents of array_1 array_2 = array_1.copy() # Setting the first element of array_2 to 10 array_2[0] = 10 print(f'Initial array: {array_1}') print(f'Modified copy: {array_2}')
copy

Now, we have created a new array for array_2 with the same elements as array_1.

For 2D arrays, the copying procedure is exactly the same.

numpy.copy() Function

Instead of the .copy() method, we can also use the copy() function, which takes the array as its parameter: array_2 = np.copy(array_1).

Both the function and the method work the same; however, there is one nuance. They both have the order parameter, which specifies the memory layout of the array, but their default values are different. If you want to read more about it, here is the documentation: ndarray.copy, numpy.copy.

Task
test

Swipe to show code editor

  1. Create a copy of sales_data_2021 using the correct method of a NumPy array and store it in sales_data_2022.

  2. Assign the NumPy array with elements 390 and 370 to the last two elements of the first row (1D array) of sales_data_2022. Use a positive index and a slice with only negative start specified for indexing sales_data_2022.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt