Tạo GeoDataFrame từ một DataFrame
Trong bài tập này, bạn sẽ tạo một GeoDataFrame của geopandas từ DataFrame Nashville Public Art. Bạn cần chỉ định CRS và định nghĩa geometry. Điều này sẽ giúp bạn sẵn sàng thực hiện spatial join giữa dữ liệu nghệ thuật và dữ liệu khu phố để khám phá khu phố nào có nhiều tác phẩm nghệ thuật nhất.
Dữ liệu Nashville Public Art đã được nạp sẵn dưới tên art.
Bài tập này là một phần của khóa học
Trực quan hóa dữ liệu không gian địa lý bằng Python
Hướng dẫn bài tập
- In
head()của dữ liệuart. - Tạo một cột geometry trong DataFrame
artbằng phương thứcpoints_from_xy()củageopandas. - Xây dựng một GeoDataFrame từ
artvà đặt tên làart_geo. Đặtcrsbằngneighborhoods.crsvà đặtgeometrybằng cột bạn vừa tạo. - In
type()củaart_geođể kiểm tra nó là một GeoDataFrame.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
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(____))