14

I have a list of dataframes, all of the same structure and all are polygons.

How can I add them all together using geopandas?

24

it seems that this is the right way to do that right now:

rdf = gpd.GeoDataFrame( pd.concat( dataframesList, ignore_index=True) )
  • 7
    Great answer, one improvement: rdf = gpd.GeoDataFrame(pd.concat(dataframesList, ignore_index=True), crs=dataframesList[0].crs). Now new dataframe will also have the same CRS as one of the initial dataframes. Because of using pandas.concat any geographical metadata such as CRS does not get preserved by default. – AleksMat Sep 12 '18 at 16:44
  • This works well - thanks. I did find the append can go astray e.g. when one file has "name" and another has "Name", I ended up with nothing in either column for rows from one file. I couldn't reproduce it reliably so I ended up forcing column names to lower case to simplify the column mashing, which seems to be reliable. – Mike Honey Nov 21 '19 at 1:38
12

I just experimented with this - maybe in GeoPandas 0.2.1 and Pandas 0.20.3 it is a bit more concise:

gdf = pd.concat([gdf1, gdf2])

gdf is automatically created as a GeoDataFrame. Of course if there is a chance of conflicting indices you'll want to keep the 'ignore_index=True' parameter.

  • 3
    I just tried this and I can confirm it does not work (using pandas_0.20.3 geopandas_0.3.0). The result is a DataFrame, not a GeoDataFrame. E.g. if you try to use gdf1.to_file(xxx, driver='ESRI Shapefile') you will get an error which says 'DataFrame' object has no attribute 'to_file' – jberrio Jul 18 '18 at 0:48
  • @jberrio thanks for the update! looks like the update to GeoPandas made things more explicit. – Charlie Parr Jul 18 '18 at 18:19
  • 1
    For the flagger that requested moderators remove the Accept checkmark please be aware that this is not possible to do. As a workaround from meta.stackexchange.com/a/187263/215590 I tried deleting the answer, but the answerer requested that it be undeleted. A long term solution for dealing with accepted answers that may no longer be correct has been proposed at meta.stackexchange.com/questions/268666/… – PolyGeo Jul 20 '18 at 23:04
  • This worked fine for me with pandas version: 0.24.2 and geopandas version: 0.5.0 – denson Jun 11 '19 at 17:42

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.