Numpy&Pandas 006: Copy

We said a simple array,

1
2
3
4
import numpy as np
a = np.arange((4))

#array([0, 1, 2, 3])

Copy

Normally, we set b is equal to a and c is equal to b.

1
2
3
4
5
b = a
c = b

#b: array([0, 1, 2, 3])
#c: array([0, 1, 2, 3])

When we change a value in array a

1
a[0] = 99

Will the array of b be changed? And c?

1
array([99,  1,  2,  3])

They all changed like above. So we are sure they have a link in each other, if we change any value of them, they all will be changed.

Deep Copy

In numpy, we have a inner function to copy and there is no any link between.

1
d = a.copy()

However we change the value of a, d will not change anything.

Thanks for your reading, I hope it is useful for you.


End

Illustrator / Cagy

Text / Cagy

Editor / Cagy

Design / Cagy