[英]Geopandas Export Changing Data Type
嗨,我在使用 python 3.7 中的 geopandas 将 shapefile 和 csv 之间的连接导出到 shapefile 时遇到问题。 我的代码如下。 当我运行它时,它会保存一个 shapefile,但是,当我尝试使用 shapefile 中的数据时,似乎各个列的数据类型正在从 int 更改为其他类型。 如何固定“Manufa Emp”的数据类型,以便在导出它时,数据类型保持为 int?
import sys
import pandas as pd
import geopandas as gpd
import numpy
# Set Working Directory
sys.path.append(r"/Users/antonioramos/Desktop/Buzard_Research_Program")
# Read in gz.csv file as "ZCTA" Table
emp = r"/Users/antonioramos/Desktop/Buzard_Research_Program/DEC_00_SF3_DP3_with_ann.csv"
Table = pd.read_csv(emp, skiprows = 1)
# Create new table "ZCTA_Manufa" with only Block ID and Total employment columns
Tab2 = Table.loc[:,["Id2", "Number; Employed civilian population 16 years and over -
INDUSTRY - Manufacturing"]].values
# renaming headers
Tab2 = pd.DataFrame(data=Tab2, columns=["ZCTA5CE00", "Manufa_Emp"])
# Import Shapefile
zips = r"/Users/antonioramos/Desktop/Buzard_Research_Program/tl_2010_06_zcta500.shp"
data = gpd.read_file(zips)
# To join the two together
Table3 = data.merge(Tab2, on='ZCTA5CE00')
zFeatures = Table3.filter(['Manufa_Emp', 'ZCTA5CE00', 'geometry'], axis = 1)
zFeatures['Manufa_Emp'].astype(int)
# Set geometry and CRS
geometry = zFeatures.geometry
geo_df = gpd.GeoDataFrame(Table3, geometry = geometry)
geo_df = geo_df.to_crs('epsg:5070')
sum(zFeatures['Manufa_Emp'])
# Export out as a shapefile
result = ("CA_ZCTA_Man6.shp")
geo_df.to_file(result)
这是我一直用于解决相同问题的解决方案。 我在下面展示了为 ESRI shapefile 制作“长整数”或“短整数”的解决方案。 这些可由 ArcGIS Pro 或 Arcmap 10.x 读取:
## For 'Short integer' format
schema = gpd.io.file.infer_schema(geo_df)
schema['properties']['Manufa_Emp'] = 'int32:4'
geo_df.to_file(DDN_shp_name, schema=schema)
## For 'Long integer' format
schema = gpd.io.file.infer_schema(geo_df)
schema['properties']['Manufa_Emp'] = 'int32:10'
geo_df.to_file(DDN_shp_name, schema=schema)
我仍然无法弄清楚如何避免 geopandas 在使用 to_file 时实现的一些字段更改,但这应该有助于整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.