
I love using Geopandas, and over the last two months, I have been sharing some of the best tips and tricks on processing geospatial data with Geopandas. This article is the second part of the series, where I will share another seven tips and tricks to make your life easier in dealing with geospatial data in Python.
The first part of this Geopandas Tips covers five pro tips, and you can read the article from the link below.
Here is the list of tips we include in this article.
- CSV to Geodataframe
- Geometry Filter
- Dissolve
- Create Bubble Maps
- Find Nearest Distance
- To Geojson/Geopackage
- Read from PostGIS
Tip #1 – CSV to Geodataframe
A lot of datasets come in CSV formats, and many of these datasets have coordinates (latitude and longitude). Converting these datasets to a Geodataframe opens up a whole lot of geospatial processing functionalities in Geopandas.
To convert a CSV to a Geodataframe, we read first the CSV file with Pandas as shown in the below snippet of code. Then, we can transform the data frame to a Geodataframe using points_from_xy function in Geopandas.

Having converted our data frame to a Geodotaframe, we can now perform geometry operations including plotting geospatial data, calculating distances and many more.
Tip #2 – Geometry Filter
With pandas, we can filter out data using rows or columns. With a Geodataframe, in addition to the pandas filtering, you have access to filter data by their geometries. The following example shows reading the famous taxi data. This dataset is a relatively large file so we can avoid reading the whole dataset using geometry filter where we read only points within Manhattan boundary.

The mask parameter takes a geometry so we are providing here a borough name and Geopandas will take care of excluding all other boroughs in reading the dataset.
Tip #3 – Dissolve
Usually, we have different polygons to work with geographic boundaries, including among other zip codes, neighbourhoods, districts, etc.. How do you merge these subunits without losing the statistical counts from the original dataset.
With non-geographic data, all we need is using groupby
function. However, with spatial data, we need to also aggregate geometric features. In that case, we can use dissolve. The following example shows dissolving country boundaries to continents

The following visualization shows the country boundaries (upper left) dissolved into continent boundaries (lower right) using the above code.

Note that you need to have a column to use the aggregation dissolve. Also, you can carry out different statistical calculations. In this example, we have shown how to calculate the sum.
Tip #4 – Create Bubble Maps
Bubble maps are an excellent alternative to choropleth maps when you have point dataset and want to visualize the quantity by size. It is not that hard to create bubble maps in Geopandas, but I believe it is not apparent how to make one.
To create a bubble map, you only need to set the marker size parameter to any quantity column as shown in the following code snippet.

Besides, you need to take care of the overlapping point circles; therefore, I set here the alpha to a low value to be transparent. You can see the output visualization.

Tip #5 – Find Nearest Distance
Distances and finding what is nearby is part and parcel of spatial analysis. In this tip, I show you how to calculate distances and find out the nearest point effectively.
Let us say; we have cities. The nearest_city function takes a Point (Latitude and Longitude) and the cities Geodataframe and returns the nearest city to the provided coordinates.

You can have your data as well. It does not have to be cities per se. But with any point dataset, you can now calculate the nearest neighbourhood using this function.
Tip #6 – To Geojson/Geopackage
Who does not like shapefiles! Besides shapefiles, we can also export geo-processed data to other formats to store locally. In this example, I show how you can store Geodataframes as GeoJSON and Geopackages locally.

With GeoJSON, you need to specify the driver as GeoJSON. Saving in Geo package format, we need to set the layer name and the driver as GPKG.
Tip #7 – Read from PostGIS
Become a power user and set up your PostGIS database. With Geopandas, you can read the data using SQL. This code snippet goes through an example of reading data from a PostgreSQL database using Geopandas and Sqlalchemy.

SQL syntax can be wrapped as strings, and Geopandas will execute it. This method is a great way to access your data through a spatial database. You can get started in installing and using PostgreSQL following this article:
Conclusion
I hope you enjoyed this round of Geospatial data processing tips in Python. In this article, we have seen seven tips to use Geopandas for geospatial data analysis effectively.
Some of these tips are directly geometric manipulations which are essential for processing Geospatial data: converting a Dataframe to Geodataframe, the geometry filter and dissolving. As a Geospatial data scientist, the heavy lifting of the geometric functionality is life-saving.
The other remaining tips touch on geospatial data visualization, spatial database connections, finding nearest neighbour and geospatial vector data format output.
If you like to follow these tips and tricks as I post them on Twitter, You can find them at @spatialML