1

I have assigned a variable aae with one column of pandas dataframe. If any modification is made on variable aae then the values of pandas dataframe get automatically modified. Please look at this example:

Screenshot of the code

modification in aae makes an automatic change in pandas dataframe value without any direct change made in df['Absorption_Angstrom_Exponent_440-870nm']. If aae is modified then pandas dataframe df should not be changed. Please help me with this automatic update issue with pandas dataframe.

Naveed
  • 11,495
  • 2
  • 14
  • 21
  • aae is a reference to the df dataframe. if you need to keep them distinct. add .copy() when assigning – Naveed Oct 14 '22 at 14:18
  • https://stackoverflow.com/questions/48201521/will-changes-in-dataframe-values-always-modify-the-values-in-the-data-frame – BigBen Oct 14 '22 at 14:19

1 Answers1

1

Don't use .values but to_numpy(copy=True):

a = df.to_numpy(copy=True)

Example:

df = pd.DataFrame(np.zeros((3, 3), dtype=int))

a = df.to_numpy(copy=True)

a[0, 0] = 999

output:

   0  1  2
0  0  0  0
1  0  0  0
2  0  0  0

Same thing with .values:

     0  1  2
0  999  0  0
1    0  0  0
2    0  0  0
mozway
  • 194,879
  • 13
  • 39
  • 75