gpt4 book ai didi

python - 将 Shapefile 读入 Dataframe

转载 作者:行者123 更新时间:2023-12-01 08:13:08 25 4
gpt4 key购买 nike

我有一个 shapefile,我想将其转换为 Python 3.7 中的数据帧。我尝试过以下代码:

import pandas as pd
import shapefile
sf_path = r'data/shapefile'
sf = shapefile.Reader(sf_path, encoding = 'Shift-JIS')

fields = [x[0] for x in sf.fields][1:]
records = sf.records()
shps = [s.points for s in sf.shapes()]

sf_df = pd.DataFrame(columns = fields, data = records)

但我收到此错误消息:

TypeError: Expected list, got _Record

那么我应该如何将列表转换为 _Record 或者有办法解决它?我也尝试过 GeoPandas,但安装时遇到了一些问题。谢谢!

最佳答案

def read_shapefile(sf_shape):
"""
Read a shapefile into a Pandas dataframe with a 'coords'
column holding the geometry information. This uses the pyshp
package
"""

fields = [x[0] for x in sf_shape.fields][1:]
records = [y[:] for y in sf_shape.records()]
#records = sf_shape.records()
shps = [s.points for s in sf_shape.shapes()]
df = pd.DataFrame(columns=fields, data=records)
df = df.assign(coords=shps)
return df

关于python - 将 Shapefile 读入 Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55112771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com