I am trying to understand how copying a pandas data frame works. When I assign a copy of an object in python I am not used to changes to the original object affecting copies of that object. For example:
x = 3
y = x
x = 4
print(y)
3
While x has subsequently been changed, y remains the same. In contrast, when I make changes to a pandas df after assigning it to a copy df1 the copy is also affected by changes to the original DataFrame.
import pandas as pd
import numpy as np
def minusone(x):
return int(x) - 1
df = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
df1 = df
print(df1['A'])
0 10
1 20
2 30
3 40
4 50
Name: A, dtype: int64
df['A'] = np.vectorize(minusone)(df['A'])
print(df1['A'])
0 9
1 19
2 29
3 39
4 49
Name: A, dtype: int64
The solution appears to be making a deep copy with copy.deepcopy(), but because this behavior is different from the behavior I am used to in python I was wondering if someone could explain what the reasoning behind this difference is or if it is a bug.