I want a copy of a dataframe which contains a dataframe. When I change something in the nested dataframe, it shouldn't change in the original dataframe.
I have a dataframe like this:
0 1 2
0 1 2 <__main__.PossibleCombinations object at 0x000...
1 4 5 6
Generated with the next code:
import copy
import numpy as np
import pandas as pd
df = pd.DataFrame(data= [[1,2,3],[4,5,6]])
class PossibleCombinations:
def __init__(self, dfCombinations, numberCombinations):
self.dfCombinations = dfCombinations
self.numberCombinations = numberCombinations
df.iloc[0,2] = PossibleCombinations(pd.DataFrame(data= [[1,2,3],[4,5,6]]),6)
print(df)
When I make a deepcopy of the hole dataframe and the nested dataframe and change something in the nested dataframe in the copy, the value also changes in the original.
deepCopy = copy.deepcopy(df)
deepCopy.iloc[0,2].dfCombinations = copy.deepcopy(df.iloc[0,2].dfCombinations)
deepCopy.iloc[0,2].dfCombinations.iloc[0,2] = "doei"
print(deepCopy.iloc[0,2].dfCombinations)
print(" ")
print(df.iloc[0,2].dfCombinations)
output:
0 1 2
0 1 2 doei
1 4 5 6
0 1 2
0 1 2 doei
1 4 5 6
but I want:
0 1 2
0 1 2 doei
1 4 5 6
0 1 2
0 1 2 3
1 4 5 6
What is the fix for this problem?