Get Started

Construct a GeoDataFrame from a DataFrame

In this exercise, you will construct a geopandas GeoDataFrame from the Nashville Public Art DataFrame. You will need to specify the CRS and define a geometry. This will get you ready to spatially join the art data and the neighborhoods data in order to discover which neighborhood has the most art.

The Nashville Public Art data has been loaded for you as art.

This is a part of the course

“Visualizing Geospatial Data in Python”

View Course

Exercise instructions

  • Print the head() of the art data.
  • construct a geometry column in the art DataFrame using the geopandas points_from_xy() method.
  • Build a GeoDataFrame using art and call it art_geo. Set crs equal to the neighborhoods.crs and set the geometry equal to the column you just created.
  • Print the type() of art_geo to verify it is a GeoDataFrame.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt

# Print the first few rows of the art DataFrame
print(art.____())

# Create a geometry column in art
art['geometry'] = gpd.____(art.lng, art.lat)

# Create a GeoDataFrame from art and verify the type
art_geo = gpd.GeoDataFrame(art, 
                           ____ = neighborhoods.crs, 
                           ____ = art.geometry)
print(type(____))

This exercise is part of the course

Visualizing Geospatial Data in Python

IntermediateSkill Level
5.0+
7 reviews

Learn how to make attractive visualizations of geospatial data in Python using the geopandas package and folium maps.

You'll work with GeoJSON to create polygonal plots, learn about projections and coordinate reference systems, and get practice spatially joining data in this chapter.

Exercise 1: GeoJSON and plotting with geopandasExercise 2: Working with GeoJSONExercise 3: ColormapsExercise 4: Map Nashville neighborhoodsExercise 5: Projections and coordinate reference systemsExercise 6: Changing coordinate reference systemsExercise 7: Construct a GeoDataFrame from a DataFrame
Exercise 8: Spatial joinsExercise 9: Spatial join practiceExercise 10: Finding the neighborhood with the most public artExercise 11: Aggregating points within polygonsExercise 12: Plotting the Urban Residents neighborhood and art

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free