解析 CSV

使用 Polars 擴充與最佳化資料管線

Liam Brannigan

Data Scientist & Polars Contributor

財務 CSV 範例擷取

Chicago Finance Department
Generated: 2026-02-01
WARD;TYPE;STATUS;REQUEST_COST
11;Pothole in Street Complaint;Completed;125
42;Street Light Out;Open;88
27;Alley Light Out;Completed;61
  • 🛑 標頭不在第 1 列
  • 🛑 欄位以分號分隔
使用 Polars 擴充與最佳化資料管線

跳過列

vendor_requests = pl.read_csv(
    "ward_service_requests.csv",
    skip_rows=2,

)
使用 Polars 擴充與最佳化資料管線

跳過多餘標頭列

vendor_requests = pl.read_csv(
    "ward_service_requests.csv",
    skip_rows=2,
    separator=";",
)
使用 Polars 擴充與最佳化資料管線

檢查解析後的檔案

vendor_requests.head(3)
shape: (3, 4)
| WARD | TYPE                        | STATUS    | REQUEST_COST |
| ---  | ---                         | ---       | ---          |
| i64  | str                         | str       | i64          |
|------|-----------------------------|-----------|--------------|
| 11   | Pothole in Street Complaint | Completed | 125          |
| 42   | Street Light Out            | Open      | 88           |
| 27   | Alley Light Out             | Completed | 61           |
使用 Polars 擴充與最佳化資料管線

結構推斷的問題

WARD;TYPE;STATUS;REQUEST_COST
11;Pothole in Street Complaint;Completed;125
42;Street Light Out;Open;88
27;Alley Light Out;Completed;61


$$

  • 前 100 列 → 推斷結構
  • 只有整數 → 欄位為 Int64
使用 Polars 擴充與最佳化資料管線

結構推斷

WARD;TYPE;STATUS;REQUEST_COST
11;Pothole in Street Complaint;Completed;125
42;Street Light Out;Open;88
27;Alley Light Out;Completed;61
...
3;Rodent Baiting Service Request;Completed;61.5

$$

  • 前 100 列 → 推斷結構
  • 只有整數 → 欄位為 Int64
使用 Polars 擴充與最佳化資料管線

結構推斷

vendor_requests = pl.read_csv(
    "ward_service_requests.csv",
    separator=";",
    skip_rows=2,
    infer_schema_length=200,
)
使用 Polars 擴充與最佳化資料管線

結構推斷

vendor_requests.schema
Schema({'WARD': Int64, 'TYPE': String, 'STATUS': String, 'REQUEST_COST': Float64})
使用 Polars 擴充與最佳化資料管線

提供結構定義

vendor_requests = pl.read_csv(
    "ward_service_requests.csv",
    separator=";",
    skip_rows=2,
    schema={
        "WARD": pl.Int64,
        "TYPE": pl.String,
        "STATUS": pl.String,
        "REQUEST_COST": pl.Float64,
    },
)
使用 Polars 擴充與最佳化資料管線

覆寫推斷的結構

vendor_requests = pl.read_csv(
    "ward_service_requests.csv",
    separator=";",
    skip_rows=2,
    schema_overrides={"REQUEST_COST": pl.Float64},
)
使用 Polars 擴充與最佳化資料管線

檢查覆寫結果

vendor_requests.schema
Schema({'WARD': Int64, 'TYPE': String, 'STATUS': String, 'REQUEST_COST': Float64})
使用 Polars 擴充與最佳化資料管線

不良資料問題

WARD;TYPE;STATUS;REQUEST_COST
11;Pothole in Street Complaint;Completed;125.0
unknown;Street Light Out;Open;88.5
27;Alley Light Out;Completed;61.0
使用 Polars 擴充與最佳化資料管線

忽略解析錯誤

pl.read_csv(
    "ward_service_requests.csv",
    separator=";",
    skip_rows=2,
    infer_schema_length=200,
    ignore_errors=True,
)

$$

  • 不良值 → null
  • ⚠ 會「隱藏」其他錯誤
使用 Polars 擴充與最佳化資料管線

標記空值

pl.read_csv(
    "ward_service_requests.csv",
    separator=";",
    skip_rows=2,
    infer_schema_length=200,
    null_values={"WARD": "unknown"},
)

$$

  • 保留「預期的 dtype」
  • ignore_errors 更安全
使用 Polars 擴充與最佳化資料管線

一起來練習吧!

使用 Polars 擴充與最佳化資料管線

Preparing Video For Download...