開始使用免費開始

擷取經度與緯度

已為你預先載入名為 df 的 DataFrame。完成程式碼,將經度與緯度擷取到新的「分開的」欄位中。

本練習屬於課程

在 Python 視覺化地理空間資料

檢視課程

練習說明

  • 使用 print()df.head() 列印前幾列。
  • 使用串列生成式將緯度擷取到名為 lat 的新欄位。
  • 使用串列生成式將經度擷取到名為 lng 的新欄位。
  • 再次使用 print()df.head() 列印前幾列,確認新欄位已建立。

動手互動練習

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

# print the first few rows of df 
print(df.head())

# extract latitude to a new column: lat
df['lat'] = [loc[_____] for loc in df.Location]

# extract longitude to a new column: lng
df['lng'] = [loc[_____] for loc in df.Location]

# print the first few rows of df again
print(df.head())
編輯並執行程式碼