省去中間人
現在你已經知道如何透過 pandas 把資料放進 Spark,但你可能會想:為什麼還要經過 pandas?直接把文字檔讀進 Spark 不就更簡單嗎?當然可以!
所幸,你的 SparkSession 有一個 .read 屬性,其中包含多種方法可以把不同資料來源讀成 Spark 的 DataFrame。用這些方法,你也能像在一般的 pandas DataFrame 那樣,從 .csv 檔建立 DataFrame!
變數 file_path 是字串,指向檔案 airports.csv 的路徑。這個檔案包含世界各地不同機場的資訊。
名為 spark 的 SparkSession 已經在你的工作區裡可用。
本練習屬於課程
PySpark 基礎
練習說明
- 使用
.read.csv()方法建立名為airports的 Spark DataFrame。- 第一個引數為
file_path。 - 傳入引數
header=True,讓 Spark 從檔案第一行讀取欄位名稱。
- 第一個引數為
- 呼叫
.show()來列印這個 DataFrame。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Don't change this file path
file_path = "/usr/local/share/datasets/airports.csv"
# Read in the airports data
airports = ____.____.____(____, ____=____)
# Show the data
____.____()