Phân tích CSV

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Liam Brannigan

Data Scientist & Polars Contributor

Bản trích CSV tài chính

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
  • 🛑 Header không ở hàng đầu
  • 🛑 Cột phân tách bằng dấu chấm phẩy
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Bỏ qua các hàng đầu

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

)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Bỏ qua các hàng header thừa

vendor_requests = pl.read_csv(
    "ward_service_requests.csv",
    skip_rows=2,
    separator=";",
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kiểm tra tệp đã phân tích

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           |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Vấn đề suy luận schema

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 hàng đầu → suy luận schema
  • Chỉ số nguyên → cột kiểu Int64
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Suy luận schema

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 hàng đầu → suy luận schema
  • Chỉ số nguyên → cột kiểu Int64
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Suy luận schema

vendor_requests = pl.read_csv(
    "ward_service_requests.csv",
    separator=";",
    skip_rows=2,
    infer_schema_length=200,
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Suy luận schema

vendor_requests.schema
Schema({'WARD': Int64, 'TYPE': String, 'STATUS': String, 'REQUEST_COST': Float64})
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Cung cấp schema

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,
    },
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Ghi đè schema suy luận

vendor_requests = pl.read_csv(
    "ward_service_requests.csv",
    separator=";",
    skip_rows=2,
    schema_overrides={"REQUEST_COST": pl.Float64},
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kiểm tra phần ghi đè

vendor_requests.schema
Schema({'WARD': Int64, 'TYPE': String, 'STATUS': String, 'REQUEST_COST': Float64})
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Vấn đề dữ liệu xấu

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
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Bỏ qua lỗi phân tích

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

$$

  • Giá trị xấu → null
  • Ẩn lỗi khác
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Đánh dấu giá trị null

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

$$

  • Giữ nguyên dtype dự kiến
  • An toàn hơn ignore_errors
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Ayo berlatih!

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Preparing Video For Download...