Geospatial Data with GeoPandas
Reviewed & published by Brayan K
GeoPandas extends pandas with a geometry column so you can work with maps, shapes, and locations. A GeoDataFrame behaves like a normal DataFrame but can read shapefiles and GeoJSON, reproject coordinates, run spatial joins, and plot maps.
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Learn what a GeoDataFrame and geometry column are, how to read spatial files, what CRS and projections mean, how spatial joins work, and how to draw a quick map.
What You'll Learn in This Lesson
- GeoDataFrames and the geometry column
- Reading shapefiles and GeoJSON
- CRS and projections (EPSG:4326)
- Reprojecting with to_crs
- Spatial joins with sjoin
- Plotting maps with plot()
1 GeoDataFrames and the Geometry Column
A GeoDataFrame is just a pandas DataFrame with one special geometry column holding Shapely shapes — points, lines, or polygons. Every other column behaves like ordinary pandas, so all your filtering and grouping skills carry over directly.
- GeoDataFrame: a DataFrame + a geometry column
- Geometry: Shapely Point, LineString, Polygon
- CRS: the coordinate reference system tying numbers to the Earth
The runnable example below uses plain pandas: storing latitude and longitude as ordinary columns. GeoPandas would wrap these into real Point geometries instead.
import pandas as pd
# Plain pandas: lat/lon as ordinary numeric columns
cities = pd.DataFrame({
"city": ["Paris", "Berlin", "Madrid"],
"lat": [48.85, 52.52, 40.42],
"lon": [2.35, 13.40, -3.70],
})
# Normal pandas works fine on the attribute columns
print(cities.sort_values("lat", ascending=False))
# In GeoPandas, lat/lon would become a geometry column of Points
print(cities[["city", "lat", "lon"]])
# ✅ Expected output:
# city lat lon
# 1 Berlin 52.52 13.40
# 0 Paris 48.85 2.35
# 2 Madrid 40.42 -3.70
# city lat lon
# 0 Paris 48.85 2.35
# 1 Berlin 52.52 13.40
# 2 Madrid 40.42 -3.702 Reading Files, CRS, and Projections
GeoPandas reads many vector formats with one call, gpd.read_file, including shapefiles and GeoJSON. Each layer carries a CRS (Coordinate Reference System); EPSG:4326 is WGS84 longitude/latitude. Use to_crs to reproject so layers share the same system before you measure or join.
3 Spatial Joins and Plotting Maps
A spatial join (gpd.sjoin) combines two layers by how their geometries relate in space — for example, which region polygon each city point falls within — rather than by a shared key. Both layers must share the same CRS. Finally, gdf.plot() draws a quick map with matplotlib.
🎯 Your Turn: Sort the Cities
Before mapping, work with the attribute columns in plain pandas. Fill in the blank to sort the cities from northernmost (highest latitude) to southernmost.
import pandas as pd
cities = pd.DataFrame({
"city": ["Paris", "Berlin", "Madrid"],
"lat": [48.85, 52.52, 40.42],
"lon": [2.35, 13.40, -3.70],
})
# 🎯 YOUR TURN: sort by 'lat' from highest to lowest
north_to_south = cities.sort_values("___", ascending=False)
print(north_to_south)
# Expected order: Berlin, Paris, Madrid📋 Quick Reference
| Task | Code (GeoPandas) |
|---|---|
| Import | import geopandas as gpd |
| Read a file | gpd.read_file("data.geojson") |
| Reproject | gdf.to_crs(3857) |
| Spatial join | gpd.sjoin(a, b, predicate="within") |
| Plot a map | gdf.plot(column="pop") |
❓ Frequently Asked Questions
You've completed the Pandas course!
Congratulations — that was the final lesson. You started with Series and DataFrames and finished with scaling tools and geospatial analysis. Along the way you mastered selection, cleaning, merging, group-by, dates, aggregation, and the capstone, then explored Dask, Polars, Spark, and GeoPandas for going bigger and further.
🚀 Keep practicing on your own datasets — real data is the best teacher. Well done!
Practice quiz
What does GeoPandas add on top of pandas?
- Geospatial data structures and operations
- GPU support
- A web server
- Image editing
Answer: Geospatial data structures and operations. GeoPandas extends pandas with geospatial types and operations on vector geometry.
What is the main GeoPandas data structure called?
- GeoSeries only
- SpatialFrame
- GeoDataFrame
- MapFrame
Answer: GeoDataFrame. A GeoDataFrame is a pandas DataFrame with a special geometry column.
What does the special geometry column hold?
- Plain numbers
- Shapely geometry objects like points, lines, polygons
- Only strings
- Colors
Answer: Shapely geometry objects like points, lines, polygons. The geometry column stores Shapely geometries such as Point, LineString, and Polygon.
Which function reads shapefiles and GeoJSON into a GeoDataFrame?
- gpd.open_map
- pd.read_csv
- gpd.load_shape
- gpd.read_file
Answer: gpd.read_file. geopandas.read_file reads many vector formats including shapefiles and GeoJSON.
What does CRS stand for in geospatial data?
- Coordinate Reference System
- Cartographic Render Style
- Central Region Storage
- Column Row System
Answer: Coordinate Reference System. A CRS (Coordinate Reference System) defines how coordinates map to locations on Earth.
Which attribute and method handle a GeoDataFrame's projection?
- .srid and .convert()
- .crs and .to_crs(...)
- .map and .warp()
- .proj and .reproject()
Answer: .crs and .to_crs(...). gdf.crs reports the projection; gdf.to_crs(...) reprojects to a different CRS.
Which method joins two layers based on their spatial relationship?
- spatial_merge()
- merge()
- concat()
- sjoin()
Answer: sjoin(). geopandas.sjoin (or gdf.sjoin) performs a spatial join using predicates like intersects or within.
Which common EPSG code represents WGS84 longitude/latitude?
- EPSG:2154
- EPSG:0000
- EPSG:4326
- EPSG:3857
Answer: EPSG:4326. EPSG:4326 is WGS84 lon/lat, the typical CRS for GPS coordinates and GeoJSON.
How do you quickly draw a map from a GeoDataFrame?
- gdf.map_it()
- gdf.plot()
- gdf.draw()
- gdf.render()
Answer: gdf.plot(). gdf.plot() renders the geometries with matplotlib, optionally colored by a column.
Why must two layers share a CRS before a spatial join?
- So coordinates are comparable in the same reference system
- To save memory
- It is not required
- For nicer colors
Answer: So coordinates are comparable in the same reference system. Spatial operations need both layers in the same CRS so their coordinates line up correctly.
Continue this course
- Previous: Pandas & Spark
- Quick reference: Pandas cheat sheet
Frequently asked questions
What is a GeoDataFrame?
A GeoDataFrame is a pandas DataFrame with one special geometry column that holds Shapely geometry objects such as points, lines, and polygons. Every other column works exactly like normal pandas, so you can filter, group, and merge while also running spatial operations on the geometry.
What is a CRS and why does it matter?
A CRS, or Coordinate Reference System, defines how the numbers in the geometry column map to real positions on Earth. EPSG:4326 is WGS84 longitude/latitude. Before measuring distances or joining layers you usually reproject with to_crs so both datasets share the same CRS and their coordinates line up.
What is a spatial join?
A spatial join (geopandas.sjoin) combines two layers based on how their geometries relate in space rather than on a shared key. For example, you can attach the name of the region polygon that each point falls within, using a predicate such as intersects or within. Both layers must share the same CRS.