【发布时间】:2020-11-09 22:01:17
【问题描述】:
您好,我在 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)
【问题讨论】:
-
不确定我们将如何诊断数据类型问题,而不会看到可重现示例中的数据。这是一个演示如何使用
StringIO类从 CSV 中创建可重现的示例:stackoverflow.com/questions/62883806/…
标签: python-3.x arcgis geopandas