開始使用免費開始

使用 pandas 轉換銷售資料

在從資料集擷取洞見之前,可能需要先調整欄位型別,才能正確運用資料。這在時間類型的資料中特別常見,因為它可能以多種方式儲存。

在這個例子中,pandas 已以 pd 匯入,隨時可供你使用。

本練習屬於課程

使用 Python 的 ETL 與 ELT

檢視課程

練習說明

  • 更新 transform() 函式,將 "Order Date" 欄位的資料轉換為 datetime 型別。
  • 篩選 DataFrame,只保留 "Price Each" 小於 10 美元的列。
  • 列印 DataFrame 中各欄位的資料型別。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

raw_sales_data = extract("sales_data.csv")

def transform(raw_data):
    # Convert the "Order Date" column to type datetime
    raw_data["Order Date"] = pd.____(____, format="%m/%d/%y %H:%M")
    
    # Only keep items under ten dollars
    clean_data = raw_data.loc[____, :]
    return clean_data

clean_sales_data = transform(raw_sales_data)

# Check the data types of each column
print(____)
編輯並執行程式碼