0

I thought that working on a copy of a pandas df didn't change the original df... However, in this example, it does:

test = pd.DataFrame({"first": [[1,2,3], [4,2,5], [6,2,7]]})
a = test.copy()
s = a.sample()
s = s["first"].iloc[0]
s.remove(2)

When I now print test again, it has changed. Why does this happen and how can I prevent it?

LizzAlice
  • 678
  • 7
  • 18
  • 3
    See here - https://stackoverflow.com/questions/59683237/deep-copy-of-pandas-dataframes-and-dictionaries – Tom Ron Dec 02 '20 at 13:53
  • 1
    An explanation can also be found at the very end of the [docs](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.copy.html). – BigBen Dec 02 '20 at 13:54

0 Answers0