Python熊猫df.copy()不深

问题描述 投票:0回答:1

(我认为)python熊猫存在一个奇怪的问题。如果我这样做:

cc1 = cc.copy(deep=True)

对于数据框cc,然后询问特定的行和列:

print(cc1.loc['myindex']['data'] is cc.loc['myindex']['data'])

我知道

True

这里怎么了?

python pandas dataframe deep-copy
1个回答
0
投票

深度复制在熊猫中不起作用,开发人员考虑将可变对象作为反模式放置在DataFrame中

您的代码中没有什么错,以防万一,如果您想了解深度复制和浅复制的示例之间的区别,就在这里。

深层复印

dict_1= {'Column A': ['House','Animal', 'car'],
     'Column B': ["walls,doors,rooms", "Legs,nose,eyes", "tires,engine" ]}

df1 = pd.DataFrame(dict_1, columns=['Column A', 'Column B'])

# Deep copy
df2 = df1.copy()  #  deep=True by default
df2 == df1  # it returns True because no updates has happened on either of dfs
output
#   Column A    Column B
# 0 True    True
# 1 True    True
# 2 True    True

id(df1)  # output: 2302063108040
id(df2)  # ouptut: 2302063137224

现在,如果您更新df1的B列,则>

dict_new =  {'Column A': ['House','Animal', 'car'],
     'Column B': ["walls", "Legs,nose,eyes,tail", "tires,engine,bonnet" ]}

# updating only column B values
df1.update(dict_new)

df1 == df2   # it returns false for the values which got changed

输出:

    Column A    Column B
0   True    False
1   True    False
2   True    False

如果我们看到深度复制的df1#,则保持不变

df1
# output:
# Column A  Column B
# 0 House   walls,doors,rooms
# 1 Animal  Legs,nose,eyes
# 2 car tires,engine

浅复制

df2 = df1.copy(deep=False)  #  deep=True by default hence explicitly providing argument to False
df2 == df1  # it returns True because no updates has happened on either of dfs
# output
#   Column A    Column B
# 0 True    True
# 1 True    True
# 2 True    True

dict_new =  {'Column A': ['House','Animal', 'car'],
     'Column B': ["walls", "Legs,nose,eyes,tail", "tires,engine,bonnet" ]}

df1.update(dict_new)

df2 == df1  # since it has same reference of d1 you will see all true even after updating column B unlike deep copy
# output
#   Column A    Column B
# 0 True    True
# 1 True    True
# 2 True    True

df2  # now if you see df2 it has all those updated values of df1

# output:
#   Column A    Column B
# 0 House   walls
# 1 Animal  Legs,nose,eyes,tail
# 2 car tires,engine,bonnet

来源:python Pandas DataFrame copy(deep=False) vs copy(deep=True) vs '='

© www.soinside.com 2019 - 2024. All rights reserved.