Python GeoPandas – How to Convert Shapefile from Pandas Back to GeoPandas

geopandaspython

I have these shapefiles:

q=gpd.read_file(a.shp)
df=gpd.read_file(b.shp)
final = pd.merge(df, q, on=['FID_buffer'], how='inner')
#out= a certain directory
final.to_file(out)

AttributeError: 'DataFrame' object has no attribute 'to_file'

I want after the join to export it back as shp

I tried with spatial join in geopandas but it said something with crs was wrong that's why I did with pandas but I can't export it.

final = gpd.sjoin(df, q, how="inner", op='intersects')

which gives:

AttributeError: 'DataFrame' object has no attribute 'crs'

Best Answer

When joining 2 GeoDataFrames by an attribute (so a merge with pandas and not a spatial join, which can certainly be sensible for a given application), the result will be a pandas DataFrame and not a GeoDataFrame.
It will also have two columns with geometry data (one of each original GeoDataFrame), and you will need to decide which of those columns you want to have as the 'active' geometry column (i.e. the column that is used for the spatial methods called on that GeoDataFrame).

Therefore, you need to explicitly convert it back to a GeoDataFrame specifying which geometry column should be used:

GeoDataFrame(final, geometry='name_of_geometry_name')
Related Question