python-3.x - Geopandas 导出更改数据类型
问题描述
嗨,我在 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 时实现的一些字段更改,但这应该有助于整数。
推荐阅读
- javascript - 如何使用 JavaScript 在 DOM 元素中迭代所有子元素,包括那些没有标签的子元素
- java - 如何将此for循环转换为java 8流模拟
- python - ValueError:调用 librosa.feature.melspectrogram 时,没有任何元素反映在“数组”的轴 0 中
- google-app-engine - 如何在 app.yaml 中为 Google appengine 中部署的 asp.net core webapi 指定网络名称
- python - 哪一个更可取?np.where 还是 .loc?
- r - 如何每次从许多不同的照片中获取相同的图像(重复代码)?
- datatable - 闪亮的模块命名空间问题:数据表中的操作按钮
- ocaml - 无法从 opam 获得最新的Why3 版本
- java - Drools:DMN 1.2 FEEL 功能的评估不起作用
- r - 如何从矩阵中调用变量...从列表中?