0

I am not sure what I am missing... Why does data change when I am manipulating data_pos and data_neg? I am running 3.8.5.

>>> import pandas as pd
>>> data = pd.DataFrame(data={'A':[-3,-2,-1,0,0,1,2,3]})
>>> print(data)
   A
0 -3
1 -2
2 -1
3  0
4  0
5  1
6  2
7  3
>>> data_pos = data
>>> data_neg = data
>>>
>>> data_pos.loc[data_pos.loc[:,'A']<0,'A']=0
>>> data_neg.loc[data_neg.loc[:,'A']>0,'A']=0
>>>
>>> print(data)
   A
0  0
1  0
2  0
3  0
4  0
5  0
6  0
7  0
>>>
PencilBox
  • 65
  • 7
  • 1
    Obligatory link to [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html) – Henry Ecker Jun 25 '21 at 01:15
  • 1
    Yes thank you, I just hope my ignorance of this fact hasn't burned me in the past without me knowing it! – PencilBox Jun 25 '21 at 01:16

1 Answers1

1

You need to add copy

data_pos = data.copy()

data_neg = data.copy()
BENY
  • 317,841
  • 20
  • 164
  • 234