0

I have run a code as follows: data_model = data2

data_model.drop("Sale",axis = 1,inplace = True) This results in the removal of sale column from the data_model dataframe as well as the data2 dataframe. why? does it act like a pointer to data2

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • 1
    Possible duplicate of [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – juanpa.arrivillaga Oct 23 '19 at 21:35
  • `data_model = data2` simply says "the name `data_model` now refers to the object referenced by `data2`. Note, in the future, please always tag any python related questions with the generic [python] tag – juanpa.arrivillaga Oct 23 '19 at 21:36
  • I did. Are you suggesting that I should put up a generic [python] tag only for such questions? – Abhik Mitra Oct 24 '19 at 22:05
  • No, you did not. You can see in the [revision history](https://stackoverflow.com/posts/58531273/revisions) that I added the generic tag. – juanpa.arrivillaga Oct 24 '19 at 23:24

1 Answers1

0

Yes, data_model = data2 is just a pointer and does not create a copy, therefore editing one is also editing the other... you have to use this:

data_model = data2.copy()

And read the docs here

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62