開始使用免費開始

將巴黎餐廳資料轉為 GeoDataFrame

在本章的第一個程式練習中,我們從一個 csv 檔案匯入了巴黎餐廳的位置。 為了啟用 GeoPandas 的空間功能,我們要把 pandas 的 DataFrame 轉成 GeoDataFrame。這可以用 GeoDataFrame() 建構子與 geopandas.points_from_xy() 函式完成,這裡已經幫你處理好了。

現在我們有了 GeoDataFrame,就能使用所有空間功能,例如繪製幾何圖形。這個練習要用餐廳資料集做出和第一個練習相同的圖,但這次改用 GeoDataFrame 的 plot() 方法。

Pandas 已以 pd 匯入,GeoPandas 以 geopandas 匯入,而 matplotlib 的 pyplot 功能以 plt 匯入。

本練習屬於課程

在 Python 中處理地理空間資料

檢視課程

練習說明

  • 檢視 restaurants GeoDataFrame 的前幾列。
  • 使用 GeoDataFrame 的 plot() 方法繪圖。其回傳值是一個 matplotlib 的座標軸物件:將它命名為 ax
  • 一樣將標記大小設為 1。
  • 使用 contextily 加上一個底圖圖層。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Read the restaurants csv file into a DataFrame
df = pd.read_csv("paris_restaurants.csv")

# Convert it to a GeoDataFrame
restaurants = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.x, df.y))

# Inspect the first rows of the restaurants GeoDataFrame
print(restaurants.____)

# Make a plot of the restaurants
ax = restaurants.____
import contextily
contextily.____(____)
plt.show()
編輯並執行程式碼