What is GeoPandas?

GeoPandas is a powerful Python library that extends pandas to handle geographic data. It combines:

Key benefits:

You can it with

pip install geopandas

And import it with:

import geopandas as gpd

Reading Geospatial Data

GeoPandas excels at reading various geospatial file formats:

Let's read a GeoJSON file of world country boundaries:

Main functions used:

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.head()
pop_estcontinentnameiso_a3gdp_md_estgeometry
0889953.0OceaniaFijiFJI5496MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
158005463.0AfricaTanzaniaTZA63177POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2603253.0AfricaW. SaharaESH907POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
337589262.0North AmericaCanadaCAN1736425MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4328239523.0North AmericaUnited States of AmericaUSA21433226MULTIPOLYGON (((-122.84000 49.00000, -120.0000...

Basic Data Manipulation

GeoPandas allows you to manipulate geometric data using familiar pandas operations:

Example: Filter to show only European countries:

europe = world[world['continent'] == 'Europe']
europe.head()
pop_estcontinentnameiso_a3gdp_md_estgeometry
18144373535.0EuropeRussiaRUS1699876MULTIPOLYGON (((180.00000 71.51571, 180.00000 ...
215347896.0EuropeNorwayNOR403336MULTIPOLYGON (((15.14282 79.67431, 15.52255 80...
4367059887.0EuropeFranceFRA2715518MULTIPOLYGON (((-51.65780 4.15623, -52.24934 3...
11010285453.0EuropeSwedenSWE530883POLYGON ((11.02737 58.85615, 11.46827 59.43239...
1119466856.0EuropeBelarusBLR63080POLYGON ((28.17671 56.16913, 29.22951 55.91834...

Key concept: GeoDataFrames behave like regular pandas DataFrames but preserve geometric data.

Visualizing Geospatial Data

GeoPandas leverages matplotlib for quick and easy map creation:

  • .plot(): Creates a plot of the GeoDataFrame
  • column: Specifies which data to use for coloring
  • cmap: Sets the color scheme
  • legend: Adds a color bar
  • figsize: Sets the figure size

Introduction to geopandas: Visualizing Geospatial Data

ax = europe.plot(column='pop_est', cmap='YlOrRd', legend=True,
            legend_kwds={'label': 'Population', 'orientation': 'horizontal'},
            figsize=(12, 8))

ax.set_xlim(-20, 180)
ax.set_ylim(30, 90)

Geospatial Operations

GeoPandas enables various geospatial operations, crucial for advanced analysis and visualizations:

  • .geometry.centroid: Calculates the geometric center of each shape
  • .plot(): Can be used multiple times on the same axis (ax) to overlay different geometries

Introduction to geopandas: Geospatial Operations

# Calculate centroids
europe['centroid'] = europe.geometry.centroid

# Plot countries and centroids
ax = europe.plot(figsize=(12, 8))
europe.centroid.plot(ax=ax, color='black', markersize=10)

ax.set_xlim(-20, 180)
ax.set_ylim(30, 90)

Going further

TLDR: geopandas allows to

Remember, GeoPandas integrates well with other Python geospatial libraries:

These examples provide a foundation, but GeoPandas offers much more:

As you explore further, you'll discover its full potential for geospatial data visualization and analysis.