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 exercise is part of the course
Visualizing Geospatial Data in Python
Exercise instructions
- Print the
head()of theartdata. - construct a geometry column in the
artDataFrame using thegeopandaspoints_from_xy()method. - Build a GeoDataFrame using
artand call itart_geo. Setcrsequal to theneighborhoods.crsand set thegeometryequal to the column you just created. - Print the
type()ofart_geoto 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(____))