How To Copy a Numpy Array Into Another Array?

Copying One Numpy Array Into Another

Arrays are a type of data structure in Python that stores objects of similar data types. But sometimes occasions may arise where you need to copy one array into another array. In this article, we are going to learn about how to copy one Numpy array into another Numpy array. So let’s get started!

What Is a Numpy Array?

An array is a type of data structure in Python that stores objects of similar data types. It is almost similar to lists except for the fact that lists can store objects of multiple data types.

For example:

import numpy as np
my_array = np.array([1,2,3,4])
print(my_array)

Output:

[1 ,2 , 3, 4]

Methods To Copy a Numpy Array Into Another Array

So let’s just right away look at the methods or functions you can use.

1. Using np.copy() function

This in-built function will return the exact same cop of the input array.

Syntax of the function is as follows:

numpy.copy(a, order=’K’)

Let’s take a look at the following example.

import numpy as np
  
# Creating a numpy array using np.array()
my_array = np.array([1.63, 7.92, 5.46, 66.8, 7.89,
                      3.33, 6.56, 50.60, 100.11])
  
print("The original array is: ")
  
print(my_array)
  
# Now copying the org_array to copy_array using np.copy() function
copy = np.copy(my_array)
  
print("\nCopied array is: ")
  
print(copy)

Output:

The original array is: 
[  1.63   7.92   5.46  66.8    7.89   3.33   6.56  50.6  100.11]

Copied array is: 
[  1.63   7.92   5.46  66.8    7.89   3.33   6.56  50.6  100.11]

2. Using the Assignment Operator 

Assignment operators are generally used in python to assign values to variables. But we can also use them to copy one array into another. 

For example:

import numpy as np
  
my_array = np.array([[100, 55, 66 ,44, 77]])
  
# Copying the original array to copy using Assignment operator
copy = my_array

print('The Original Array: \n', my_array)
  

print('\nCopied Array: \n', copy)

Output:

The Original Array: 
 [[100  55  66  44  77]]

Copied Array: 
 [[100  55  66  44  77]]

Here we have simply assigned the original array to the copied array. 

3. Using np.empty_like function

In this method, we will first create an empty array like the original array and then we will assign the original array to the empty array.

Syntax of the function is as follows:

numpy.empty_like(a, dtype = None, order = ‘K’)

Let’s take a look at the following example.

import numpy as np
  
my_ary = np.array([34, 65, 11, 
                66, 80, 630, 50])
  
print("The original array is:")
  
print(my_ary)
  
# Creating an empty Numpy array similar to the original array
copy = np.empty_like(my_ary)
  
# Assigning my_ary to copy
copy[:] = my_ary
  
print("\nCopy of the original array is: ")
  
print(copy)

Output:

The original array is:
[ 34  65  11  66  80 630  50]

Copy of the original array is: 
[ 34  65  11  66  80 630  50]

Conclusion

In summary, we learned about different methods and functions we can use to copy one array into another. Arrays are a very useful data structure and knowing the different operations you can perform on an array is very important.