Python 将shapefile读入数据帧
我有一个shapefile,我想在Python3.7中将其转换为dataframe。我尝试了以下代码:Python 将shapefile读入数据帧,python,dataframe,type-conversion,shapefile,Python,Dataframe,Type Conversion,Shapefile,我有一个shapefile,我想在Python3.7中将其转换为dataframe。我尝试了以下代码: 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.point
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
那么,我应该如何将列表转换为_记录,或者有办法解决它?我也尝试过GeoPandas,但在安装时遇到了一些问题。谢谢 我也遇到了同样的问题,这是因为.shp文件在每个记录中都有一种键字段,当转换为dataframe时,需要一个列表,并且只找到该字段,测试更改:
records = [y[:] for y in sf.records()]
我希望这能奏效 什么类型的
shapefile
?据说在我的编辑器中,需要输入的是一个列表
@Yusufsn,它显示“记录”是一个“_记录”列表。shapefile是一个包含经度/纬度和其他信息的几何文件。请提供shapefile
中的输入示例?
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