將銷售資料載入至 CSV 檔案
在任何資料管線中,載入資料都是不可或缺的一環。這能確保任何資料消費者與流程,都能可靠地存取你先前在管線中擷取並轉換過的資料。在本練習中,你將練習使用 pandas(已匯入為 pd)把轉換後的銷售資料載入到 CSV 檔案。此外,原始資料已經完成擷取,並可在 DataFrame raw_sales_data 中使用。
本練習屬於課程
使用 Python 的 ETL 與 ELT
練習說明
- 篩選
raw_sales_dataDataFrame,只保留價格低於 25 美元的所有項目。 - 更新
load()函式,將轉換後的銷售資料寫入名為"transformed_sales_data.csv"的檔案,並確保不要包含index欄位。 - 對清理後的 DataFrame 呼叫
load()函式。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def transform(raw_data):
# Find the items prices less than 25 dollars
return raw_data.loc[raw_data["Price Each"] ____ ____, ["Order ID", "Product", "Price Each", "Order Date"]]
def load(clean_data):
# Write the data to a CSV file without the index column
____.____("transformed_sales_data.csv", index=____)
clean_sales_data = transform(raw_sales_data)
# Call the load function on the cleaned DataFrame
____(____)