How to Concatenate GeoDataFrames in Python?

Estimated read time 2 min read

To concatenate GeoDataFrames in Python, you can use the concat() function provided by the geopandas library. The concat() function allows you to concatenate multiple GeoDataFrames along a specific axis. Here’s an example:

import geopandas as gpd

# Create two GeoDataFrames
gdf1 = gpd.GeoDataFrame({'City': ['London', 'Paris'], 'Geometry': [Point(0, 0), Point(1, 1)]})
gdf2 = gpd.GeoDataFrame({'City': ['Berlin', 'Madrid'], 'Geometry': [Point(2, 2), Point(3, 3)]})

# Concatenate GeoDataFrames
concatenated_gdf = gpd.concat([gdf1, gdf2])

# Print the concatenated GeoDataFrame
print(concatenated_gdf)

In this example, we create two GeoDataFrames, gdf1 and gdf2, with city names and corresponding point geometries. We then use the concat() function from geopandas to concatenate the two GeoDataFrames into a single GeoDataFrame, concatenated_gdf. The concat() function takes a list of GeoDataFrames to concatenate as its first argument.

Note that when concatenating GeoDataFrames, it’s important to ensure that they have the same coordinate reference system (CRS) and compatible geometry types. If the GeoDataFrames have different CRS or geometry types, you may need to perform conversions or modifications before concatenation.

After concatenation, you can work with the resulting GeoDataFrame, concatenated_gdf, which will contain all the features from the original GeoDataFrames combined.

Make sure you have the geopandas library installed in your Python environment to use the concat() function and work with GeoDataFrames.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply