# Python Copy NumPy Array

In this Python NumPy tutorial, we will learn how do we copy a NumPy array in Python. With the Python NumPy copy function, we will cover these topics.

• Python copy numpy array not reference
• Python copy numpy array to clipboard
• Python duplicate numpy array
• Python copy list to numpy array
• Python copy part of numpy array
• Python copy data to numpy array
• Python deep copy numpy array

## Python copy numpy array

• In this section, we will discuss how to copy a numpy array with another array in Python numpy array.
• In Python, the numpy module provides a numpy.copy() function and this function will help the user to copy a numpy array into another.
• For example, we have an original array and we want to get the new instance from the original array. In simple words, we can say copy array data to another one.

Syntax:

Let’s have a look at the Syntax and understand the working of Python numpy.copy() function.

``````numpy.copy
(
a,
order='K',
subok=False
)``````
• It consists of a few parameters
• a: This parameter indicates the input array data
• order: By default it takes ‘K’ value and that represents the layout of array.
• subok: This is an optional parameter and by default it takes ‘False’ value.

Example:

Let’s take an example and check how to copy the numpy array into another by using the numpy.copy() function

Source Code:

``````import numpy as np

new_arr= np.array([15,27,38])
print("Creation of array:",new_arr)
result=np.copy(new_arr)
print("Copying of array:",result)``````

In the above code first, we imported the numpy library and then use the np.array() function for creating a numpy array. After that, we declared a variable ‘result’ and assign a numpy.copy() function. This function will help the user to return a new numpy array with the exact element values as that of the original array.

Here is the implementation of the following given code.

Also, read: Check if NumPy Array is Empty in Python

## Python copy numpy array not reference

In this example, we are going to use the deepcopy() method because shallow copy stores the reference of objects in the memory, and the deep copy generate a new compound object.

Example:

``````import numpy as np
import copy

array1= np.array([178,289,934,670])
print("Creation of array:",array1)
new_output = copy.deepcopy(array1)
print(new_output)``````

In the above code, we have used the copy.deepcopy() method and within this method we have passed the array as an argument. Once you will print ‘new_output’ then the result will display the elements which are present in the original array.

You can refer to the below Screenshot.

## Python copy numpy array to clipboard

• In this Program, we will discuss how to use the clipboard for copying the original elements to another in Python numpy array.
• To perform this particular task first we will import the pandas and numpy library and then initialize an array by using the np.array() function. Next, we will declare a variable ‘df’ and assign the pd.dataframe() function in which we have to assign the array as an argument.

Source Code:

``````import numpy as np
import pandas as pd

new_arr=np.array([16,23,45,67,89])
df = pd.DataFrame(new_arr)
df.to_clipboard(index=True)
print(df)``````

Here is the Screenshot of the following given code.

## Python duplicate numpy array

• In this section, we will discuss how to get the duplicate values from an original array.
• In Python the numpy.tile() function is used to repeat the number of values present in an array. For example suppose we have a numpy array that contains [16, 56, 92,67] then this function will help the user to get the duplicate elements one time and make a new numpy array.
• This method is available in the NumPy package module and always returns the tiled output array.

Syntax:

Here is the Syntax of numpy.tile() function

``````numpy.tile
(
A,
reps
)``````
• It consists of a few parameters
• A: This parameter defines the input array.
• reps: This parameter indicates how to repeat the array and number of repetition along with axis.

Example:

Let’s take an example and understand the working of Python numpy.tile() function.

Source Code:

``````import numpy as np

new_arr = np.array([16, 56, 92,67])
print("Creation of array:",new_arr)
result=np.tile(new_arr, 1)
print("Duplicated values:",result)``````

In the following given code, we have just simply multiplied a numpy array by using the numpy.tile() function. Firstly we have imported the numpy library and then initialize an array. After that use the numpy.tile() function and pass ‘new_arr’ and repetition value as an argument.

Here is the execution of the following given code.

As you can see in the Screenshot the output displays the new duplicated array from the original array.

## Python copy list to numpy array

• Here we can see how to copy the list elements with a numpy array in Python.
• To do this task we are going to use the numpy.asarray() function. In Python the numpy.asarray() function is used to convert the instance object with numpy array and the object will be list, tuple, string and this method is available in the NumPy package module.
• This method takes few parameters and always returns a numpy array that contains all the values of the Python list.

Syntax:

Let’s have a look at the syntax and understand the working of numpy.asarray() function.

``````numpy.asarray
(
a,
dtype=None,
order=None,
*``````
• It consists of a few parameter
• a: This parameter indicates the input data which we want to convert with array.
• dtype: By default it takes none value and the datatype is inferred from the input data.
• order: It is an optional parameter and it is used for column-major or row-major.

Example:

Let’s take an example and check how to copy the elements from a list and store them into an array.

Source Code:

``````import numpy as np

new_list=[16,25,87,93]
print("Creation of list:",new_list)
result=np.asarray(new_list)
print("Copy elements into array:",result)``````

In the above code, we imported the numpy library and then initialize the input data that is listed. Next, we have to use the np.asarray() function and it will convert the list into an array with the same elements.

Here is the implementation of the following given code.

## Python copy part of numpy array

• In this Program, we will discuss how to copy the elements of numpy array in Python.
• By using the assignment operator we can easily perform this task. First, we will import the numpy library and then create an array by using the np.array() function. After that, we have declared a variable ‘new_arr2’ and assigned the original array ‘new_arr’.
• Once you will print ‘new_arr2’ then the output will display the copy of the numpy original array.

Source Code:

``````import numpy as np

new_arr = np.array([[8, 2, 3],
[4, 7, 6]])
print("Creation of array:",new_arr)
new_arr2 = new_arr
print(" Coping elements into another array:",new_arr2)``````

You can refer to the below Screenshot.

## Python copy data to numpy array

• In this section we will discuss how to copy data from numpy array in Python.
• To perform this particular task we are going to use the numpy.empty_like() function. In Python, this function is used to declare a new array with the same size and shape as the original array.
• This method is available in the Numpy package module and the datatype of the prototype defines the same attribute of the returned numpy array.

Syntax:

Here is the Syntax of Python numpy.empty_like() function.

``````numpy.empty_like
(
prototype,
dtype=None,
order='K',
subok=True,
shape=None,
)``````
• It consists of a few parameters
• prototype: This parameter indicates the attributes of the returned numpy array.
• order: By default it takes ‘K’ value and it can be either ‘C’ or ‘F’ contiguous.
• dtype: It is an optional parameter and it takes none value.

Example:

Let’s take an example and check how to copy data from a numpy array in Python.

Source Code:

``````import numpy as np

new_arr = ([17, 34, 98],
[56, 192, 345])
print("Creation of array:",new_arr)
new_output = np.empty_like(new_arr, dtype = int)
new_output[:]=new_arr
print("Copy array:",new_output)``````

In the following given code, we imported the numpy library and then initialize an array. After that, we declare a variable and assign np.empty_like() function and within this function, we have passed the array as an argument.

Next, we have assigned an array to copy and once you will print ‘new_output‘ then the result will display the same array.

Here is the execution of the following given code

## Python deep copy numpy array

• In this section, we will discuss how to use the deep.copy() function in Python NumPy array.
• In Python, the deepcopy() method is used to copy each element of the given array into the copied array.
• This method is available in the NumPy package module and it creates a new object and adds copy values recursively. This function takes an array as an argument and returns a deep copy of the numpy array.

Source Code:

``````import numpy as np
import copy

new_arr= np.array([45,26,78,91])
print("Creation of array:",new_arr)
result = copy.deepcopy(new_arr)
print(result)``````

Here is the Screenshot of the following given code.