This article has participated in the weekend study program, click the link to see details: Weekend study

(a)

When reading a specified array from TXT using a Boolean index, some methods modify the original array and some do not. It turned out to be a deep and shallow copy problem.

Import copy module first:

import copy
Copy the code

Then create list A

A = [1, 2, 3]Copy the code

Then assign a to B:

b = a
print(b)
Copy the code

Output:

A direct assignment is essentially a reference (alias) to the object. The simplest way is to insert something into b and print a and b:

Changing B causes A to change too (shallow copy). This is a shallow copy, which copies the parent object rather than its internal children. We implement deepcopy from c to a using the copy.deepcopy method:

Deepcopy, using the copy module’s deepcopy method, copies the parent object and its children. If you can’t remember any of the above, remember:

Shallow copy, a change all change. Deep copy, separate from each other.

(2)

In addition, the copying method in Pandas is very efficient.

DataFrame.copy(deep=True)

Copy the index and data of this object.

When deep=True (the default), a new object is created using a copy of the calling object’s data and index. Changes to the data or index of the replica are not reflected in the original object (see comments below).

When deep=False, a new object is created without copying the data or index of the calling object (only references to the data and index are copied). Any changes to the original data will be reflected in the shallow copy (and vice versa).

Notes

When deep=True, the data is copied but the actual Python object is not copied recursively, only referred to that object. This contrasts with copy.deepCopy in the standard library, which copies object data recursively.

Deep =True when Index is copied, the underlying NUMpy array is not copied for performance reasons. Because indexes are immutable, the underlying data can be shared safely and no copies are required.

Reference documentation