掃描多個檔案
團隊的借閱資料現在分散在每年一個 CSV(seattle_2021.csv、seattle_2022.csv、seattle_2023.csv)。這些年度檔案使用舊版欄位名稱 usageclass 與 materialtype。請使用萬用字元樣式一次掃描所有檔案,視為單一邏輯資料集,接著建立實體館藏借閱的摘要。
polars 已載入為 pl,目錄位於 MULTIFILE_DIR。
本練習屬於課程
使用 Polars 擴充與最佳化資料管線
練習說明
- 使用萬用字元樣式,在
MULTIFILE_DIR中掃描每個seattle_*.csv檔案。 - 將合併後的資料集篩選為
"Physical"借閱,然後依materialtype分組。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Scan every yearly file using a glob pattern
yearly_checkouts = pl.____(
str(MULTIFILE_DIR / "____")
)
# Build a physical-checkout summary across the combined dataset
result = (
yearly_checkouts
# Filter to physical
.filter(pl.col("usageclass") == "____")
.group_by("____")
.agg(pl.col("checkouts").sum().alias("total"))
.sort("total", descending=True)
.collect()
)
print(result)