将Shapefile读取到Dataframe中

时间:2019-03-12 01:23:24

标签: python dataframe type-conversion shapefile

我有一个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,但安装时遇到了一些麻烦。谢谢!

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,这是因为 .shp 文件在每个记录中都有一种键字段,并且在转换为数据框时,需要一个列表,并且仅找到该字段,然后进行测试更改:

router.get('/', function(req, res, next) {
  Category.find({}, function(error, data){
    // something 
  });

  Product.find({}, function(error, data){
    // something
  }); 

  res.render('produtos/index', { 
    categories: Categories,
    Products: Products
  });
});

我希望这行得通!

答案 1 :(得分:0)

def read_shapefile(sf_shape):     “”     使用“ coords”将shapefile读取到Pandas数据框中     包含几何信息的列。这使用了pyshp     包     “”“

$query_pagination = "SELECT ap.id AS `appointments_id`, us.* FROM appointments AS ap, users AS us WHERE us.id = ap.user_id AND us.nev LIKE '%".$search."%'"

while($row = mysqli_fetch_array($result))
{
    echo $row["appointments_id"]; //i want to echo here the id from appointments
}