Skip to content

Instantly share code, notes, and snippets.

@eug
Created September 11, 2017 01:24
Show Gist options
  • Save eug/f556c43352ca0efcd17e9828a73a2744 to your computer and use it in GitHub Desktop.
Save eug/f556c43352ca0efcd17e9828a73a2744 to your computer and use it in GitHub Desktop.
Convert a ESRI Shapefile to Pandas DataFrame
# -*- coding: utf-8 -*-
import pandas as pd
from shapefile import Reader as read_shp
def shp_to_df(shp_filepath):
data = {}
shp = read_shp(shp_filepath)
fields = shp.fields[1:] # Skip 'DeletionFlag'
records = shp.records()
for f in fields:
data[f[0]] = []
for record in records:
for k, v in zip(fields, record):
if type(v) == bytes:
v = v.decode('ISO-8859-1') # or 'UTF-8'
data[k[0]].append(v)
return pd.DataFrame(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment