自訂 CSV 檔案
有時候你需要用自訂的方式把資料儲存在 CSV 檔案中。這可能包含使用不同的標題列值、是否包含 DataFrame 的索引欄,或是更改用來分隔欄位的字元。在這個例子中,你會練習這些操作,並確保檔案被儲存在正確的檔案路徑下。
pandas 函式庫已匯入為 pd,而且資料已轉換為只包含「Quantity Ordered」大於 1 的列。清理後的 DataFrame 儲存在名為 clean_sales_data 的變數中。
本練習屬於課程
使用 Python 的 ETL 與 ELT
練習說明
- 匯入
os函式庫。 - 將清理後的 DataFrame 寫出為儲存在
path_to_write的 CSV,且不包含標題列。 - 確認檔案已寫入到指定路徑。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the os library
____
# Load the data to a csv file with the index, no header and pipe separated
def load(clean_data, path_to_write):
clean_data.____(____, header=____, sep="|")
load(clean_sales_data, "clean_sales_data.csv")
# Check that the file is present.
file_exists = os.____.____(____)
print(file_exists)