用 pandas 載入 JSON 資料
在開發資料管線時,你可能需要處理非表格型資料與資料來源,例如 API 或 JSON 檔案。本練習將示範如何使用 pandas 從 JSON 檔案擷取資料。
pandas 已經以 pd 匯入,你要載入的 JSON 檔案路徑是 "testing_scores.json"。
本練習屬於課程
使用 Python 的 ETL 與 ELT
練習說明
- 更新
extract()函式,將 JSON 檔讀入為pandas的 DataFrame,並以 records 為導向。 - 將路徑
testing_scores.json傳入extract()函式,並把輸出存到名為raw_testing_scores的變數中。 - 印出
raw_testing_scoresDataFrame 的前幾列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def extract(file_path):
# Read the JSON file into a DataFrame
return pd.____(____, orient="____")
# Call the extract function with the appropriate path, assign to raw_testing_scores
____
# Output the head of the DataFrame
print(raw_testing_scores.____())