Can someone help me understand what is happening here:
a = 1
b = a
b = 2
print(a)
print(b)
Here, obviously a
will be unchanged because assigning 2
to b
does not alter a
.
In pandas, however:
a = pd.DataFrame({'a':[1,2,3]})
b = a
b.iloc[0,0] = 100
print(a)
print(b)
Now why do both a
and b
have 100
instead of 1
? I just found out I had been overwriting my original variables when I thought I was creating a new object in this way in pandas and had to use b = a.copy()
to avoid it.