The Wayback Machine - https://web.archive.org/web/20240620232618/https://www.geeksforgeeks.org/how-to-copy-numpy-array-into-another-array/
Open In App

How to Copy NumPy array into another array?

Last Updated : 10 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Many times there is a need to copy one array to another. Numpy provides the facility to copy array using different methods. In this

Copy NumPy Array into Another Array

There are various ways to copies created in NumPy arrays in Python, here we are discussing some generally used methods for copies created in NumPy arrays those are following.

Copy 1D Numpy Arrays into Another Using np.copy() Function

In the below example, the given Numpy array ‘org_array‘ is copied to another array ‘copy_array‘ using np.copy () function.

Python3




# importing Numpy package
import numpy as np
 
# Creating a numpy array using np.array()
org_array = np.array([1.54, 2.99, 3.42, 4.87, 6.94,
                      8.21, 7.65, 10.50, 77.5])
 
print("Original array: ")
 
# printing the Numpy array
print(org_array)
 
# Now copying the org_array to copy_array
# using np.copy() function
copy_array = np.copy(org_array)
 
print("\nCopied array: ")
 
# printing the copied Numpy array
print(copy_array)


Output:

Original array: 
[ 1.54 2.99 3.42 4.87 6.94 8.21 7.65 10.5 77.5 ]
Copied array:
[ 1.54 2.99 3.42 4.87 6.94 8.21 7.65 10.5 77.5 ]

Copy Given 2-D Array to Another Array Using np.copy() Function

In this example, the given 2-D Numpy array ‘org_array‘ is copied to another array ‘copy_array‘ using np.copy () function.

Python3




# importing Numpy package
import numpy as np
 
# Creating a 2-D numpy array using np.array()
org_array = np.array([[23, 46, 85],
                      [43, 56, 99],
                      [11, 34, 55]])
 
print("Original array: ")
 
# printing the Numpy array
print(org_array)
 
# Now copying the org_array to copy_array
# using np.copy() function
copy_array = np.copy(org_array)
 
print("\nCopied array: ")
 
# printing the copied Numpy array
print(copy_array)


Output:

Original array: 
[[23 46 85]
[43 56 99]
[11 34 55]]
Copied array:
[[23 46 85]
[43 56 99]
[11 34 55]]

NumPy Array Copy Using Assignment Operator

In the below example, the given Numpy array ‘org_array‘ is copied to another array ‘copy_array‘ using Assignment Operator.

Python3




# importing Numpy package
import numpy as np
 
# Create a 2-D Numpy array using np.array()
org_array = np.array([[99, 22, 33],
                      [44, 77, 66]])
 
# Copying org_array to copy_array
# using Assignment operator
copy_array = org_array
 
# modifying org_array
org_array[1, 2] = 13
 
# checking if copy_array has remained the same
 
# printing original array
print('Original Array: \n', org_array)
 
# printing copied array
print('\nCopied Array: \n', copy_array)


Output:

Original Array: 
[[99 22 33]
[44 77 13]]
Copied Array:
[[99 22 33]
[44 77 13]]


Previous Article
Next Article

Similar Reads

copy in Python (Deep Copy and Shallow Copy)
In Python, Assignment statements do not copy objects, they create bindings between a target and an object. When we use the = operator, It only creates a new variable that shares the reference of the original object. In order to create "real copies" or "clones" of these objects, we can use the copy module in Python. Syntax of Python DeepcopySyntax:
5 min read
NumPy ndarray.copy() Method | Make Copy of a Array
The ndarray.copy() method returns a copy of the array. It is used to create a new array that is a copy of an existing array but does not share memory with it. This means that making any changes to the original array won't affect the existing array. Example C/C++ Code # Python program explaining # numpy.ndarray.copy() function import numpy as geek x
2 min read
Difference Between Shallow copy VS Deep copy in Pandas Dataframes
The pandas library has mainly two data structures DataFrames and Series. These data structures are internally represented with index arrays, which label the data, and data arrays, which contain the actual data. Now, when we try to copy these data structures (DataFrames and Series) we essentially copy the object's indices and data and there are two
4 min read
Shallow copy vs Deep copy in Pandas Series
The pandas library has mainly two data structures DataFrames and Series. These data structures are internally represented with index arrays, which label the data, and data arrays, which contain the actual data. Now, when we try to copy these data structures (DataFrames and Series) we essentially copy the object's indices and data and there are two
4 min read
Copy a Python Dictionary and only Edit the Copy
Copying a dictionary in Python to preserve the original while making changes to the duplicate is a common task. Developers use methods like the `copy` module or the dictionary's `copy()` method to create an independent duplicate, allowing for isolated edits. This article explores commonly used techniques to copy a dictionary and modify only the dup
4 min read
Python Dictionary Copy Vs Deep Copy
In Python, Dictionary like other non-primitive data structures can be copied using various methods. The key distinction lies in the depth of the copy. In this article, we are going to learn about deep copy and shallow copy in Python. What is Shallow Copy in Python Dictionary?In Python, a shallow copy creates a new object that references the same un
3 min read
NumPy Copy and View of Array
While working with NumPy, you might have seen some functions return the copy whereas some functions return the view. The main difference between copy and view is that the copy is the new array whereas the view is the view of the original array. In other words, it can be said that the copy is physically stored at another location and the view has th
4 min read
Python - Copy contents of one file to another file
Given two text files, the task is to write a Python program to copy contents of the first file into the second file. The text files which are going to be used are second.txt and first.txt: Method #1: Using File handling to read and append We will open first.txt in 'r' mode and will read the contents of first.txt. After that, we will open second.txt
2 min read
Python | How to copy data from one excel sheet to another
In this article, we will learn how to copy data from one excel sheet to destination excel workbook using openpyxl module in Python. For working with excel files, we require openpyxl, which is a Python library that is used for reading, writing and modifying excel (with extension xlsx/xlsm/xltx/xltm) files. It can be installed using the following com
3 min read
Copy all files from one directory to another using Python
In this article, we will discuss how to copy all files from one directory to another using Python. This can be done using the shutil module. This module can be used in Python to perform operations on files and folders in a directory. Shutil package facilitates the access, movement, and removal of the files between directories. Method 1 : Using shut
2 min read
Practice Tags :