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”
Exercise instructions
- Print the
head()
of theart
data. - construct a geometry column in the
art
DataFrame using thegeopandas
points_from_xy()
method. - Build a GeoDataFrame using
art
and call itart_geo
. Setcrs
equal to theneighborhoods.crs
and set thegeometry
equal to the column you just created. - Print the
type()
ofart_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
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 DataFrameExercise 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 artWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.