從 DataFrame 建立 GeoDataFrame
在這個練習中,你要從 Nashville Public Art 的 DataFrame 建立一個 geopandas 的 GeoDataFrame。你需要指定 CRS,並定義一個 geometry。這會讓你能把藝術資料與里鄰資料做空間連接,找出哪個里鄰擁有最多公共藝術。
Nashville Public Art 資料已載入為 art。
本練習屬於課程
在 Python 視覺化地理空間資料
練習說明
- 列印
art資料的head()。 - 使用
geopandas的points_from_xy()方法,於artDataFrame 建立一個 geometry 欄位。 - 使用
art建立一個 GeoDataFrame,命名為art_geo。將crs設為neighborhoods.crs,並把geometry設為你剛建立的那個欄位。 - 列印
type()(以art_geo為引數)來驗證它是 GeoDataFrame。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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(____))