0

I am trying to save a pandas dataframe column instance. but the instance disappears once I make an update in that column, I can't get access to the old instance anymore.

C= DF.loc[:,'10']
print(C)
DF.loc[:,'10'] = None
print(C)

In the first print I get the last instance of DF.loc[:,'10'], in the second one I get a None column. How should I do to save the old instance to use it later? Does python make an update on variables of the dataframe even when I already saved it? Thanks for helping.

petezurich
  • 9,280
  • 9
  • 43
  • 57
echtouka
  • 72
  • 4

1 Answers1

0

In python, setting a variable actually sets a reference to the variable. Variable names in python are references to the original.

The following code should make a copy of C and store in D

C= DF.loc[:,'10']
print(C)
D= DF.loc[:,'10'].copy()

DF.loc[:,'10'] = None
print(C)
Irfanuddin
  • 2,295
  • 1
  • 15
  • 29
  • can you please explain the phenomen why does python updates on variables? Thank you – echtouka Oct 01 '18 at 16:12
  • @echtouka That is because in python setting a variable actually sets a reference to the variable. Variable names in python are references to the original. I would recommend you to read the answer in [this](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – Irfanuddin Oct 01 '18 at 16:38