解析 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
  • 🛑 表头不在首行
  • 🛑 列以分号分隔
使用 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 扩展与优化数据流水线

¡Vamos a practicar!

使用 Polars 扩展与优化数据流水线

Preparing Video For Download...