python - Python pandas df.copy() 不深
问题描述
我(在我看来)python pandas 有一个奇怪的问题。如果我做:
cc1 = cc.copy(deep=True)
对于数据框 cc 而不是询问某个行和列:
print(cc1.loc['myindex']['data'] is cc.loc['myindex']['data'])
我明白了
True
这里有什么问题?
解决方案
深度复制在 pandas 中不起作用,开发人员考虑将可变对象放在 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
现在,如果您更新 B 列df1
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 '=' https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html
推荐阅读
- r - 类似于 Excel 中的 AVERAGEIFS 的条件平均值
- ios - 如何从本地文件(角度)在nativescript中插入图像
- python - 使用python存档中包含的信息重命名zipfile
- java - 添加 JFreeChart 和 JCommon 库后无法访问类型 org.jfree.chart.JFreeChart
- r - 更改 R 的版本是否也需要更改 Rstudio 版本?
- docker - 将standalone.xml 添加到JBoss WildFly Docker 容器
- swift - 在第一个 viewController 中按下 UItableView 滚动时,如何使用 NSPredicate 获取请求并用数据填充第二个 viewController
- r - 缺少上限时仍绘制 CI 的下限
- angular - AOt 编译没有这样的文件或目录
- c++ - aws-sdk-cpp 如何使用 c++ sdk 的 KMSCLIENT 解密由 kms 加密的密码