首页 > 解决方案 > geopandas:创建的克隆会破坏父文件的属性

问题描述

我已经执行了以下代码,不幸的是,当我编辑另一个变量时,父变量数据被修改了。

代码如下:

filename="../foobar.shp"     #........ import any shapefile

db_file1 = geopandas.GeoDataFrame.from_file(filename)

db_file2=db_file1  #Equating one geopandas to another,essentially creating a copy

db_file2.code=(db_file2.code*2) #Where "code" is a valid column header

print (db_file2,"\n",db_file1)

输出显示 db_file1 已被修改,即使没有做任何类似的事情。我在这里做错了什么?

标签: pythongeopandas

解决方案


您没有复制,您将两个变量指向内存中的同一个对象。为了制作副本,您应该使用

df2 = df.copy()


推荐阅读