【问题标题】:Geopandas Export Changing Data TypeGeopandas 导出更改数据类型
【发布时间】: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


【解决方案1】:

这是我一直用于解决相同问题的解决方案。我在下面展示了为 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 时实现的某些字段更改,但这应该有助于整数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多