Skip to content

Instantly share code, notes, and snippets.

@aerispaha
Created January 14, 2017 19:09
Show Gist options
  • Select an option

  • Save aerispaha/f098916ac041c286ae92d037ba5c37ba to your computer and use it in GitHub Desktop.

Select an option

Save aerispaha/f098916ac041c286ae92d037ba5c37ba to your computer and use it in GitHub Desktop.
Read a shapefile into a Pandas dataframe
def read_shapefile(shp_path):
"""
Read a shapefile into a Pandas dataframe with a 'coords' column holding
the geometry information. This uses the pyshp package
"""
import shapefile
#read file, parse out the records and shapes
sf = shapefile.Reader(shp_path)
fields = [x[0] for x in sf.fields][1:]
records = sf.records()
shps = [s.points for s in sf.shapes()]
#write into a dataframe
df = pd.DataFrame(columns=fields, data=records)
df = df.assign(coords=shps)
return df
@IceMerman
Copy link
Copy Markdown

I get an error on python 3.6
Solution by rewriting
records = sf.records()
as
records =[list(i) for i in sf.records()]

@harshpme
Copy link
Copy Markdown

harshpme commented May 7, 2021

it worked!

@elvinado
Copy link
Copy Markdown

elvinado commented Aug 6, 2021

Thanks, This is useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment