I´d like to construct a shapefile from a Pandas Data Frame using the lon & lat rows.

I have got a csv file and I process it with pandas to make a data frame which is easier to handle

Is it posible to do that without make a loop line by line ?

  • If it's csv, could you use Add XY Data in ArcMap to create a layer, then export as a shapefile (rt-click layer and select data, export data)? – mkennedy May 13 '15 at 18:57
  • Thanks.But I'd like To do that making a python script. No need GIS – kamome May 13 '15 at 19:00
  • 1
    use GeoPandas – gene May 13 '15 at 19:01
  • Ah, got it. Maybe I'll delete my comments! – mkennedy May 13 '15 at 19:01

A little late but maybe also useful for others. Yes, that can be done with shapely and geopandas.

Supposed that your pandas dataframe kind of looks like this:

import pandas as pd
data = [
        {'some_attribute': 'abc', 'lat': '50.1234', 'lon': '10.4023'},
        {'some_attribute': 'def', 'lat': '40.5678', 'lon': '8.3365'},
        {'some_attribute': 'ghi', 'lat': '60.9012', 'lon': '6.2541'},
        {'some_attribute': 'jkl', 'lat': '45.3456', 'lon': '12.5478'},
        {'some_attribute': 'mno', 'lat': '35.7890', 'lon': '14.3957'},

df = pd.DataFrame(data)


       lat      lon some_attribute
0  50.1234  10.4023            abc
1  40.5678   8.3365            def
2  60.9012   6.2541            ghi
3  45.3456  12.5478            jkl
4  35.7890  14.3957            mno

First, make sure that geopandas and shapely are installed properly which sometimes is not easy because they come with some dependencies (e.g. GEOS and GDAL). If does not work at first try via pip install geopandas shapely, search for the error on Google or StackOverflow/Gis.Stackexchange because most probably there will be an answer available solving that problem for you.

Then, it is just a matter of creating a new geometry column in your dataframe which combines the lat and lon values into a shapely Point() object. Note that the Point() constructor expects a tuple of float values, so conversion must be included if the dataframe's column dtypes are not already set to float.

from shapely.geometry import Point

# combine lat and lon column to a shapely Point() object
df['geometry'] = df.apply(lambda x: Point((float(x.lon), float(x.lat))), axis=1)

Now, convert the pandas DataFrame into a GeoDataFrame. The geopandas constructor expects a geometry column which can consist of shapely geometry objects, so the column we created is just fine:

import geopandas
df = geopandas.GeoDataFrame(df, geometry='geometry')

To dump this GeoDataFrame into a shapefile, use geopandas' to_file() method (other drivers supported by Fiona such as GeoJSON should also work):

df.to_file('MyGeometries.shp', driver='ESRI Shapefile')

And that is what the resulting shapefile looks like when visualized with QGIS:

Resulting shapefile

  • Hi, I have a similar situation,but instead of points, I have polygons. It is possible to do something similar df['geometry'] = df.apply(lambda x: Point((float(x.lon), float(x.lat))), axis=1) but with polygons? – Valerio D. Ciotti Aug 23 '16 at 17:48

For ArcMap you need to define the Projection before exporting to Shapefile.

import geopandas
df = geopandas.GeoDataFrame(df, geometry='geometry')

# proj WGS84

df.crs= "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

df.to_file('MyGeometries.shp', driver='ESRI Shapefile')
  • I did something similar, using df.crs= "+init=epsg:27700" to project my shapefile according to the British National Grid. However, when I open it in ArcGIS, the coordinate system is unknown. What am I doing wrong? – FaCoffee Sep 18 '17 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.