將資料持久化到檔案
將資料載入最終目的地是資料管線中最重要的步驟之一。在本練習中,你會使用下方的 transform() 函式,先轉換產品銷售資料,再將其載入到 .csv 檔。這能讓下游的資料使用者更清楚地檢視一系列產品的總銷售額。
在這個練習中,銷售資料已經載入並完成轉換,存放在 clean_sales_data DataFrame 中。pandas 套件已以 pd 匯入,且 os 程式庫也可直接使用!
本練習屬於課程
使用 Python 的 ETL 與 ELT
練習說明
- 更新
load()函式,將資料寫入指定路徑,且不包含標題列與索引欄。 - 檢查檔案是否已寫入到預期的檔案路徑。
- 呼叫該函式,將轉換後的資料載入到持久性儲存。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def load(clean_data, file_path):
# Write the data to a file
clean_data.to_csv(file_path, ____, ____)
# Check to make sure the file exists
file_exists = os.____.____(____)
if not file_exists:
raise Exception(f"File does NOT exists at path {file_path}")
# Load the transformed data to the provided file path
____(clean_sales_data, "transformed_sales_data.csv")