Created
September 11, 2017 01:24
-
-
Save eug/f556c43352ca0efcd17e9828a73a2744 to your computer and use it in GitHub Desktop.
Convert a ESRI Shapefile to Pandas DataFrame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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