開始使用免費開始

從 DataFrame 建立 GeoDataFrame

在這個練習中,你要從 Nashville Public Art 的 DataFrame 建立一個 geopandas 的 GeoDataFrame。你需要指定 CRS,並定義一個 geometry。這會讓你能把藝術資料與里鄰資料做空間連接,找出哪個里鄰擁有最多公共藝術。

Nashville Public Art 資料已載入為 art

本練習屬於課程

在 Python 視覺化地理空間資料

檢視課程

練習說明

  • 列印 art 資料的 head()
  • 使用 geopandaspoints_from_xy() 方法,於 art DataFrame 建立一個 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(____))
編輯並執行程式碼