如何制作包含数据帧的数据帧的深度副本?(python)
我想要一个包含数据帧的数据帧的副本。当我在嵌套数据框中更改某些内容时,它不应该在原始数据框中更改 我有这样一个数据帧:如何制作包含数据帧的数据帧的深度副本?(python),python,pandas,dataframe,deep-copy,Python,Pandas,Dataframe,Deep Copy,我想要一个包含数据帧的数据帧的副本。当我在嵌套数据框中更改某些内容时,它不应该在原始数据框中更改 我有这样一个数据帧: 0 1 2 0 1 2 <__main__.PossibleCombinations object at 0x000... 1 4 5 6 当我对孔数据框和嵌
0 1 2
0 1 2 <__main__.PossibleCombinations object at 0x000...
1 4 5 6
当我对孔数据框和嵌套数据框进行深度复制并在
在副本中嵌套dataframe时,值在原始中也会更改
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)
输出:
0 1 2
0 1 2 doei
1 4 5 6
0 1 2
0 1 2 doei
1 4 5 6
3
0 1 2
0 1 2 doei
1 4 5 6
0 1 2
0 1 2 3
1 4 5 6
但我想:
0 1 2
0 1 2 doei
1 4 5 6
0 1 2
0 1 2 3
1 4 5 6
此问题的解决方案是什么?这是解决方案:
import pickle
deepCopy = pickle.loads(pickle.dumps(df))
deepCopy.iloc[0,2].dfCombinations.iloc[0,2] = "doei"
print(deepCopy.iloc[0,2].dfCombinations)
print(" ")
print(df.iloc[0,2].dfCombinations)
输出:
0 1 2
0 1 2 doei
1 4 5 6
0 1 2
0 1 2 doei
1 4 5 6
3
0 1 2
0 1 2 doei
1 4 5 6
0 1 2
0 1 2 3
1 4 5 6
这就解决了问题